Sign in to follow this  
Followers 0
jimbob

DirRemove by date

7 posts in this topic

Hi All,

I'm using the DirRemove command to remove directories once copying a new directory has been successful. All folders are named by date. Once a new copy has been success (local drive to network drive) the folder will be given today's date. Once it has been successful I would like it to remove all folders older than the current from the directory.

The date format is YYYYMMDD.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Do you mean that the old folder is renamed to the date?

If so then

;
$sWorking = @WorkingDir
FileChangeDir(@ScriptDir)
$hFind = FileFindFirstFile("*")
$iCompare = Number(@Year & @Mon & @MDay)
If $hFind <> -1 Then
   While 1
      $sFile = FileFindNextFile($hFind)
      If StringInStr(FileGetAttrib($sFile), "D") Then
         $iDate = Number(StringLeft(FileGetTime($sFile, 0, 1), 8))
         If $iDate < $iCompare Then
            ;;  Replace this MsgBox with the DirRemove() code
            MsgBox(0, $iCompare, $sFile & @CRLF & $iDate)
            ;; DirRemove($sFile, 1)
         EndIf
      EndIf
   WEnd
EndIf

FileClose($hFind)
FileChangeDir($sWorking)
MsgBox(0, "Finished", "The operation has been completed.")
;
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

I've written a script to automate a backup process. Once the user clicks "backup" it will copy files from certain directories to the network under "backup\(todays date)". Once a new copy has been successful i would like it to delete any old backups in the backup folder.

Share this post


Link to post
Share on other sites

Bump.

Anyone?

Share this post


Link to post
Share on other sites

BumpX2

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Well... You didn't really say whether GeoSoft's example worked, or what it didn't do that you needed it to yet so...

Here's his example with the ExitLoop it was missing to let you escape the While...Wend loop.

Bear in mind, his code is pulling the Modified date from the folder and using that to do the comparison.

$sWorking = @WorkingDir
FileChangeDir(@ScriptDir)
$hFind = FileFindFirstFile("*")
$iCompare = Number(@Year & @Mon & @MDay)
If $hFind <> -1 Then
   While 1
      $sFile = FileFindNextFile($hFind)
      If @error Then ExitLoop
      If StringInStr(FileGetAttrib($sFile), "D") Then
         $iDate = Number(StringLeft(FileGetTime($sFile, 0, 1), 8))
         If $iDate < $iCompare Then
            ;;  Replace this MsgBox with the DirRemove() code
            MsgBox(0, $iCompare, $sFile & @CRLF & $iDate)
            ;; DirRemove($sFile, 1)
         EndIf
      EndIf
   WEnd
EndIf

FileClose($hFind)
FileChangeDir($sWorking)
MsgBox(0, "Finished", "The operation has been completed.")

Alternatively, this does a comparison based on the foldername and the format that you specified.

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

$iCompare = @Year & "/" & @Mon & "/" & @MDay
$array =_FileListToArray (@WorkingDir, "*", 2)

For $x = 1 to $array[0]
    If StringRegExp ($array[$x], "[0-9]{8}", 0) Then
        $varFolderName = StringMid ($array[$x], 1, 4) & "/" & StringMid ($array[$x], 5, 2) & "/" & StringMid ($array[$x], 7, 2)
        If _DateDiff ("D", $varFolderName, $iCompare) > 0 Then
            MsgBox (0, "Folder to Remove", 'Folder To Remove: "' & $array[$x] & '"')
            ; DirRemove line commented for testing.
;~          DirRemove($array[$x], 1)
        EndIf
    EndIf
Next
MsgBox(0, "Finished", "The operation has been completed.")
Edited by exodius

Share this post


Link to post
Share on other sites

...

I managed to get the second one working seamlessly. Many thanks!

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