Jump to content

Move files created yesterday


skysel
 Share

Recommended Posts

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!

Link to comment
Share on other sites

  • Replies 54
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...