Sign in to follow this  
Followers 0
skysel

Find files created 1 month before current month :)

17 posts in this topic

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
        FileCopy($srcfolder & "\" & $FileList[$i], @UserProfileDir & "\Local Settings\Temp" & "\avayalogs")
    EndIf
Next

This is script for getting files created today. I haven't been using autoit now for quite some time, and now I don't know how to get list of all the files created month before current month? for example, i would like to move all files created in September to another folder... And keep the files created in October :)

any help will be much appreciated.

Share this post


Link to post
Share on other sites



Look at _FileListToArray().

If you need recursive version then search forum for _FileListToArrayEx().

I did this with a file archiver. It loops through and deletes files older than 30 days.

func file_del()
    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 thirty days
    If _DateDiff('d', $fDate, _NowCalc()) > 30 Then  ; the time
        If StringInStr(FileGetAttrib($sourceFolder & $fileList[$X]), 'D') Then
            FileWriteLine($Log_deletes,"Deleting directory " & $fileList[$X])
            ;DirRemove($sourceFolder, 1)
        Else
            FileWriteLine($Log_deletes,"Deleting file " & $fileList[$X])
            ;FileDelete($sourceFolder & "\" & $fileList[$X])
            
        EndIf 
    EndIf
    
Next
    EndFunc

Share this post


Link to post
Share on other sites

I've modified your script a bit... but it isn't working. Just wanted to do a simple test for filemove first, but it fails. and files are way older than 1 month :) suggestions?

#include <date.au3>
#include <array.au3>
#include <file.au3>

$sourcefolder = "C:\aaa"
$fileList = _FileListToArray($sourceFolder, "*", 1)


func file_del()
    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 thirty days
    If _DateDiff('d', $fDate, _NowCalc()) > 30 Then  ; the time
        If StringInStr(FileGetAttrib($sourceFolder & $fileList[$X]), 'D') Then
            ;FileWriteLine($Log_deletes,"Deleting directory " & $fileList[$X])
            FileMove($sourceFolder & "\" & $fileList[$X],"C:\bbb")
            ;DirRemove($sourceFolder, 1)
        Else
            ;FileWriteLine($Log_deletes,"Deleting file " & $fileList[$X])
            ;FileMove($sourceFolder & "\" & $fileList[$X],"C:\bbb")
            ;FileDelete($sourceFolder & "\" & $fileList[$X])
            
        EndIf 
    EndIf
Next
EndFunc

Share this post


Link to post
Share on other sites

Try this... modified version of_FileListToArray

#include <Array.au3>

$folder = FileSelectFolder("","")

$file_list = _FileListToArraySpecial($folder)

_ArrayDisplay($file_list)


Func _FileListToArraySpecial($sPath, $sFilter = "*", $iFlag = 0)
    Local $hSearch, $sFile, $asFileList[1]
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
    If (StringMid($sPath, StringLen($sPath), 1) = "\") Then $sPath = StringTrimRight($sPath, 1) ; needed for Win98 for x:\  root dir
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch = -1 Then Return SetError(4, 4, "")
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then
            SetError(0)
            ExitLoop
        EndIf
        $creation = FileGetTime ($sPath & "\" & $sFile,1) ; added line - get creation time
        If $creation[1] < @MON Then ContinueLoop ; if less then current month, ignore
        If $iFlag = 1 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
        If $iFlag = 2 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
        $asFileList[0] += 1
        If UBound($asFileList) <= $asFileList[0] Then ReDim $asFileList[UBound($asFileList) * 2]
        $asFileList[$asFileList[0]] = $sFile
    WEnd
    FileClose($hSearch)
    ReDim $asFileList[$asFileList[0] + 1] ; Trim unused slots
    Return $asFileList
EndFunc
Try this.. it

Hi ;)

Share this post


Link to post
Share on other sites

Hmm, this should do the trick, but moves also files created this month... must be just some small correction?

#include <Array.au3>

$folder = "C:\aaa"

$file_list = _FileListToArraySpecial($folder)

For $i = 1 to UBound($file_list) - 1
FileMove($folder & "\" & $file_List[$i],"c:\bbb")
Next


Func _FileListToArraySpecial($sPath, $sFilter = "*", $iFlag = 0)
    Local $hSearch, $sFile, $asFileList[1]
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringInStr($sFilter, "|")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
    If (StringMid($sPath, StringLen($sPath), 1) = "\") Then $sPath = StringTrimRight($sPath, 1) ; needed for Win98 for x:\  root dir
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch = -1 Then Return SetError(4, 4, "")
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then
            SetError(0)
            ExitLoop
        EndIf
        $creation = FileGetTime ($sPath & "\" & $sFile,1) ; added line - get creation time
        If $creation[1] < @MON Then ContinueLoop ; if less then current month, ignore
        If $iFlag = 1 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
        If $iFlag = 2 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
        $asFileList[0] += 1
        If UBound($asFileList) <= $asFileList[0] Then ReDim $asFileList[UBound($asFileList) * 2]
        $asFileList[$asFileList[0]] = $sFile
    WEnd
    FileClose($hSearch)
    ReDim $asFileList[$asFileList[0] + 1] ; Trim unused slots
    Return $asFileList
EndFunc

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

My bad, I misread your question... look for this line:

If $creation[1] < @MON Then ContinueLoop ; if less then current

change it to:

Last month only...

If $creation[1] <> @MON - 1 Then ContinueLoop

Or

Created in September (9) only...

If $creation[1] <> 9 Then ContinueLoop
Edited by Mison

Hi ;)

Share this post


Link to post
Share on other sites

My bad, I misread your question... look for this line:

If $creation[1] < @MON Then ContinueLoop ; if less then current

change it to:

Last month only...

If $creation[1] <> @MON - 1 Then ContinueLoop

Or

Created in September (9) only...

If $creation[1] <> 9 Then ContinueLoop

Well that didn't do the trick (tried multiple scenarios). Let me just clear out my question, I want all the files that haven't been created in this current month, to be moved to another folder :)

Share this post


Link to post
Share on other sites

Ohh... it looks like my first interpretation that was correct. Well.. if

If $creation[1] < @MON
won't works.. then I cannot think of any other way.. sorry :)

Hi ;)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

$sFile = FileFindFirstFile(@DesktopDir & "\*.*")
If $sFile <> -1 Then
    $iMoved = 0
    While 1
        $hFile = FileFindNextFile($sFile)
        If @Error Then ExitLoop
        $aDate = FileGetTime(@DeskTopDir & "\" & $hFile, 1)
        If $aDate[1] < @Mon OR $aDate[0] < @Year Then
            $iMoved += 1
            ;; Replace the MsgBox with the code you want to use for moving the file
            MsgBox(0, "Result", @DesktopDir & "\" & $hFile)
        EndIf
    WEnd
EndIf
MsgBox(0, "Finished", $iMoved & " older files were found")

EDIT: Or even simpler

$sFile = FileFindFirstFile(@DesktopDir & "\*.*")
If $sFile <> -1 Then
    $iMoved = 0
    While 1
        $hFile = FileFindNextFile($sFile)
        If @Error Then ExitLoop
        If StringLeft(FileGetTime(@DeskTopDir & "\" & $hFile, 1, 1), 6) < @YEAR & @MON Then
            $iMoved += 1
            ;; Replace the MsgBox with the code you want to use for moving the file
            MsgBox(0, "Result", @DesktopDir & "\" & $hFile)
        EndIf
    WEnd
EndIf
MsgBox(0, "Finished", $iMoved & " older files were found")
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Alright, so can you explain to me why it works with @DeskTopDir and not with $logpath? :)

$logpath = "C:\aaa"

$sFile = FileFindFirstFile($logpath & "\*.*")
If $sFile <> -1 Then
    $iMoved = 0
    While 1
        $hFile = FileFindNextFile($sFile)
        If @Error Then ExitLoop
        If StringLeft(FileGetTime($logpath & "\" & $hFile, 1, 1), 6) < @YEAR & @MON Then
            $iMoved += 1
            ;; Replace the MsgBox with the code you want to use for moving the file
            MsgBox(0, "Result", $logpath & "\" & $hFile)
        EndIf
    WEnd
EndIf
MsgBox(0, "Finished", $iMoved & " older files were found")

Share this post


Link to post
Share on other sites

Make sure that in your actual code that $logpath doesn't already have a trailing backslash. If it does then my code is doubling up on them, also it's a good idea to check that the folder actually exists.

$logpath = "C:\aaa"
If NOT FileExists($Logpath) Then MsgBox(0, "Error", "Unable to locate" & @CRLF & $logpath)

$sFile = FileFindFirstFile($logpath & "\*.*")
If $sFile <> -1 Then
    $iMoved = 0
    While 1
        $hFile = FileFindNextFile($sFile)
        If @Error Then ExitLoop
        If StringLeft(FileGetTime($logpath & "\" & $hFile, 1, 1), 6) < @YEAR & @MON Then
            $iMoved += 1
            ;; Replace the MsgBox with the code you want to use for moving the file
            MsgBox(0, "Result", $logpath & "\" & $hFile)
        EndIf
    WEnd
EndIf
MsgBox(0, "Finished", $iMoved & " older files were found")

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Make sure that in your actual code that $logpath doesn't already have a trailing backslash. If it does then my code is doubling up on them, also it's a good idea to check that the folder actually exists.

$logpath = "C:\aaa"
If NOT FileExists($Logpath) Then MsgBox(0, "Error", "Unable to locate" & @CRLF & $logpath)

$sFile = FileFindFirstFile($logpath & "\*.*")
If $sFile <> -1 Then
    $iMoved = 0
    While 1
        $hFile = FileFindNextFile($sFile)
        If @Error Then ExitLoop
        If StringLeft(FileGetTime($logpath & "\" & $hFile, 1, 1), 6) < @YEAR & @MON Then
            $iMoved += 1
            ;; Replace the MsgBox with the code you want to use for moving the file
            MsgBox(0, "Result", $logpath & "\" & $hFile)
        EndIf
    WEnd
EndIf
MsgBox(0, "Finished", $iMoved & " older files were found")

Ah, my fault. When I copied those files, it changed "creation date" to them. However, windows explorer still showed they were created in June 2009. When I checked details, it said October 2009.. So I guess this topic can be closed now :) Thanks for your help, very much appreciated.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

@Skysel

PMing me, or anyone else, for personal assistance is not a good move and could easily get you on someones blocked list. I'll have a look at the problem in your message AFTER I have my morning coffee and post the answer in this thread.

EDIT:

Questions answered in PMs don't help the next person with a similar problem who is taking the time to search for an answer before starting another thread.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Change the path for $logpath in the code below to your actual path containing the files you want moved and then Run the script. It will put some info on the clipboard, paste that into your next reply.

$logpath = "C:"
If Not FileExists($logpath) Then MsgBox(0, "Error", "Unable to locate" & @CRLF & $logpath)
$aMatched = _FilexDateToArray($logpath)


Func _FilexDateToArray($s_Path)
    Local $hTimer = TimerInit(), $sRtn = "", $aRtn
    If StringRight($s_Path, 1) <> "\" Then $s_Path &= "\"
    Local $sFile = FileFindFirstFile($s_Path & "*")
    If $sFile <> -1 Then
        While 1
            $hFile = FileFindNextFile($sFile)
            If @error Then ExitLoop
            If StringLeft(FileGetTime($s_Path & $hFile, 1, 1), 6) < @YEAR & @MON Then
                $sRtn &= $s_Path & "|"
            EndIf
        WEnd
        $aRtn = StringSplit(StringTrimRight($sRtn, 1), "|", 2)
        ClipPut(UBound($aRtn) & " files in " & Round(TimerDiff($hTimer) & " ms", 0))
    EndIf
EndFunc   ;;<===>_FilexDateToArray

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Change the path for $logpath in the code below to your actual path containing the files you want moved and then Run the script. It will put some info on the clipboard, paste that into your next reply.

$logpath = "C:"
If Not FileExists($logpath) Then MsgBox(0, "Error", "Unable to locate" & @CRLF & $logpath)
$aMatched = _FilexDateToArray($logpath)


Func _FilexDateToArray($s_Path)
    Local $hTimer = TimerInit(), $sRtn = "", $aRtn
    If StringRight($s_Path, 1) <> "\" Then $s_Path &= "\"
    Local $sFile = FileFindFirstFile($s_Path & "*")
    If $sFile <> -1 Then
        While 1
            $hFile = FileFindNextFile($sFile)
            If @error Then ExitLoop
            If StringLeft(FileGetTime($s_Path & $hFile, 1, 1), 6) < @YEAR & @MON Then
                $sRtn &= $s_Path & "|"
            EndIf
        WEnd
        $aRtn = StringSplit(StringTrimRight($sRtn, 1), "|", 2)
        ClipPut(UBound($aRtn) & " files in " & Round(TimerDiff($hTimer) & " ms", 0))
    EndIf
EndFunc   ;;<===>_FilexDateToArray

Hi, you're absolutely right on PM-ing, sometimes I just want things to be done too fast :)

Here is the data provided:

"40 files in 0"

How is this relevant to high CPU usage?

Edited by skysel

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Hi, you're absolutely right on PM-ing, sometimes I just want things to be done too fast :)

Here is the data provided:

"40 files in 0"

How is this relevant to high CPU usage?

I'm trying to pin down where the problem is and as part of that process I was tring to get an idea of how many file we are talking about.

Done fast? Not too likely when apperently we are on opposite sides of the world. Your first post just caught me when I was up very late at night.

Now a PM will be okay because I need some info that understandably you don't want to post on the forums.

  • The actual path for $logfile
  • The code you used for the move.
  • The type (extension) and approximate sizes of the files you have to move.
That info should allow me to find your high CPU load and either fix it or write a new move function for you. Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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  
Followers 0