Jump to content
Sign in to follow this  
skysel

Move files created yesterday

Recommended Posts

skysel

This is the part of script I use for moving files created today.

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime[0] = @YEAR And $FileTime[1] = @MON And $FileTime[2] = @MDAY Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\NetShare\TraceLogs")
    EndIf
Next

I would appreciate some pointers how to modify this script to move files created yesterday?

Thanks in advance!

Share this post


Link to post
Share on other sites
water

Please have a look at _DateAdd. This lets you calculate a new date.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
James

Or you could just do @MDay - 1. It works for me.

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime[0] = @YEAR And $FileTime[1] = @MON And $FileTime[2] = (@MDAY - 1) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\NetShare\TraceLogs")
    EndIf
Next

Share this post


Link to post
Share on other sites
water

Are you sure? What if the script was run on the 1st of February. Then the month should be decremented as well.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
James

Are you sure? What if the script was run on the 1st of February. Then the month should be decremented as well.

Ahh yes, I completely over looked that. I think it's the small radiation dose I just had :huggles: In which case, this:

#include <Date.au3>

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime[0] = _DateAdd("D", -1, _NowCalcDate()) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\NetShare\TraceLogs")
    EndIf
Next

:D

Edit: Wrong function name Posted Image

Edited by JamesBrooks

Share this post


Link to post
Share on other sites
water

I think you've got a lot of radiation :D

You have to use _DateAdd (even to subtract) and compare the whole date (not just the year).

So - if I haven't made a mistake - the code should look like:

#include <Date.au3>

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime[0] & "/" & $FileTime[1] & "/" $FileTime[2] = _DateAdd("D", -1, _NowCalcDate()) Then
    FileMove($srcfolder & "\" & $FileList[$i], "\\NetShare\TraceLogs")
    EndIf
Next
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
James

Your code is wrong too. You're again only comparing the day, it won't ever get to the months etc. How about we just compare a string?

#include <Date.au3>

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 1)
    If $FileTime = _DateAdd("D", -1, _NowCalcDate()) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\NetShare\TraceLogs")
    EndIf
Next
You could also do this with the _DateDiff function:
#include <Date.au3>

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 1)
    If _DateDiff("D", $FileTime, _NowCalcDate()) = 1 Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\NetShare\TraceLogs")
    EndIf
Next
Edited by JamesBrooks

Share this post


Link to post
Share on other sites
skysel

So which one is correct? :D

Share this post


Link to post
Share on other sites
skysel

Ok, so here is the complete script, I've modified it with your help - yet it does not work. It should move files created today and after that..files created yesterday.

#Include <File.au3>
#include <Array.au3>
#include <Date.au3>

$srcfolder = "C:\Program Files\Microsoft Dynamics CRM\Trace"

$FileList = _FileListToArray($srcfolder)

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime[0] = @YEAR And $FileTime[1] = @MON And $FileTime[2] = @MDAY Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\Tracelogs")
    EndIf
    
    If $FileTime = _DateAdd("D", -1, _NowCalcDate()) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\TraceLogs")
    EndIf
Next

Share this post


Link to post
Share on other sites
James

That would be because in my example I set FileGetTime to return a string. All you need to do is re-set the variable to the new FileGetTime everytime you go through the loop:

#Include <File.au3>
#include <Array.au3>
#include <Date.au3>

$srcfolder = "C:\Program Files\Microsoft Dynamics CRM\Trace"

$FileList = _FileListToArray($srcfolder)

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime[0] = @YEAR And $FileTime[1] = @MON And $FileTime[2] = @MDAY Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\Tracelogs")
    EndIf
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 1)    
    If $FileTime = _DateAdd("D", -1, _NowCalcDate()) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\TraceLogs")
    EndIf
Next

Share this post


Link to post
Share on other sites
Xenobiologist

Hi,

#include <Array.au3>
#include <Date.au3>
#include <File.au3>

;Root folder
$sourceFolder = @ScriptDir & '\'

;Gather files into an array
$fileList = _FileListToArray($sourceFolder, "*", 1)
Dim $found[1]

;Loop through array
For $X = 1 To $fileList[0]
    ;Retrieve creation time of file
    $Date = FileGetTime($sourceFolder & "\" & $fileList[$X], 1, 0)
    ;Format date for use with Date UDF
    $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])
    ;Calculate age, remove files older than seven days
    If _DateDiff('d', $fDate, _NowCalc()) > 7 Then ; the time
        ; here the doing
        ;FileDelete($sourceFolder & "\" & $fileList[$X])
        _ArrayAdd($found, $sourceFolder & $fileList[$X])
        ;MsgBox(1, "Files deleted:", $fileList[$X], 1)
    EndIf
    $found[0] = UBound($found)
Next
_ArrayDisplay($found)

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
skysel

That would be because in my example I set FileGetTime to return a string. All you need to do is re-set the variable to the new FileGetTime everytime you go through the loop:

#Include <File.au3>
#include <Array.au3>
#include <Date.au3>

$srcfolder = "C:\Program Files\Microsoft Dynamics CRM\Trace"

$FileList = _FileListToArray($srcfolder)

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime[0] = @YEAR And $FileTime[1] = @MON And $FileTime[2] = @MDAY Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\Tracelogs")
    EndIf
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 1)    
    If $FileTime = _DateAdd("D", -1, _NowCalcDate()) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\TraceLogs")
    EndIf
Next

Hey James, for some reason it still won't work. Any other thoughts?

Share this post


Link to post
Share on other sites
James

Hey James, for some reason it still won't work. Any other thoughts?

Very strange. Which one doesn't work? Try this:

#Include <File.au3>
#include <Array.au3>
#include <Date.au3>

$srcfolder = "C:\Program Files\Microsoft Dynamics CRM\Trace"

$FileList = _FileListToArray($srcfolder)

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime[0] = @YEAR And $FileTime[1] = @MON And $FileTime[2] = @MDAY Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\Tracelogs")
    EndIf
    
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 1)    
    ConsoleWrite("->File time: " & $FileTime & @CRLF)
    If $FileTime = _DateAdd("D", -1, _NowCalcDate()) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\TraceLogs")
    EndIf
Next

Share this post


Link to post
Share on other sites
water

Yes, but $FileTime returns YYYYMMDDHHMMSS and _DateAdd returns YYYY/MM/DD[ HH:MM:SS] (according to the help file).

As I understand it they have to be converted to another format so you can compare them.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
James

Don't want to be pedantic, but ... :D

My code works just fine as soon as you enter a "&" in line 5. :huggles:

I concatenate Year, Month and Day from Filegettime and compare it to the date returned by _DateAdd.

Which is what setting the flag in FileGetTime does.

Share this post


Link to post
Share on other sites
James

What about this then?

#Include <File.au3>
#include <Array.au3>
#include <Date.au3>

$srcfolder = "C:\Program Files\Microsoft Dynamics CRM\Trace"

$FileList = _FileListToArray($srcfolder)

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime = _NowCalcDate() Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\Tracelogs")
    EndIf
    
    If $FileTime = _DateAdd("D", -1, _NowCalcDate()) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\TraceLogs")
    EndIf
Next

Share this post


Link to post
Share on other sites
skysel

Still not working. I have full permissions to read/write to/from folders specified in this script.

Any more thoughts? :D

What about this then?

#Include <File.au3>
#include <Array.au3>
#include <Date.au3>

$srcfolder = "C:\Program Files\Microsoft Dynamics CRM\Trace"

$FileList = _FileListToArray($srcfolder)

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 0)
    If $FileTime = _NowCalcDate() Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\Tracelogs")
    EndIf
    
    If $FileTime = _DateAdd("D", -1, _NowCalcDate()) Then
        FileMove($srcfolder & "\" & $FileList[$i], "\\Netshare\TraceLogs")
    EndIf
Next

Share this post


Link to post
Share on other sites
water

This works (on a german system) - has been tested and returns some debugging information on the console:

#Include <File.au3>
#include <Array.au3>
#include <Date.au3>

$srcfolder = "C:\temp"

$FileList = _FileListToArray($srcfolder)

For $i = 1 To UBound($FileList) - 1
    $FileTimeArray = FileGetTime($srcfolder & "\" & $FileList[$i])
    $FileTime = $FileTimeArray[0] & "/" & $FileTimeArray[1] & "/" & $FileTimeArray[2]
    ConsoleWrite("Filetime: " & $FileTime & " DateAdd: " & _DateAdd("D", -1, _NowCalcDate()) & @CRLF)
    If $FileTime = _DateAdd("D", -1, _NowCalcDate()) Then
        ConsoleWrite($FileList[$i] & " was modified yesterday" & @CRLF)
    EndIf
Next


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×