Jaymac Posted March 17, 2009 Share Posted March 17, 2009 (edited) I have backup up folders that contains exchange files, which are set to create a new folder when backup is successful. My question is: can I remove folder that are older than 7 day's with a script? I have a mapped drive and the folders are in a folder called exchange. Any help would be greatly appreciated Edited March 17, 2009 by Jaymac Link to comment Share on other sites More sharing options...
Szhlopp Posted March 17, 2009 Share Posted March 17, 2009 I have backup up folders that contains exchange files, which are set to create a new folder when backup is successful.My question is: can I remove folder that are older than 7 day's with a script? I have a mapped drive and the folders are in a folder called exchange.Any help would be greatly appreciatedHey Jaymac,Take a look in the AutoIt helpfile for these two commands:FileFindFirstFileFileGetTimeWith some coding of those two functions you should be able to easily do what you want=)Szhlopp RegEx/RegExRep Tester!Nerd Olympics - Community App!Login UDFMemory UDF - "Game.exe+753EC" - CE pointer to AU3Password Manager W/ SourceDataFiler - Include files in your au3!--- Was I helpful? Click the little green '+' Link to comment Share on other sites More sharing options...
Jaymac Posted March 17, 2009 Author Share Posted March 17, 2009 Hey Jaymac,Take a look in the AutoIt helpfile for these two commands:FileFindFirstFileFileGetTimeWith some coding of those two functions you should be able to easily do what you want=)SzhloppAwesome, thanks a lot - I really appreciate it. Link to comment Share on other sites More sharing options...
SpookMeister Posted March 17, 2009 Share Posted March 17, 2009 This might help you out:#118151 [u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote] Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 17, 2009 Share Posted March 17, 2009 #include <Array.au3> #include <Date.au3> #include <File.au3> ;Root folder $sourceFolder = @ScriptDir & '\' ;Gather files into an array $fileList = _FileListToArray($sourceFolder, "*", 1) ; 2 = folders! 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 ;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 More sharing options...
Jaymac Posted March 17, 2009 Author Share Posted March 17, 2009 #include <Array.au3> #include <Date.au3> #include <File.au3> ;Root folder $sourceFolder = @ScriptDir & '\' ;Gather files into an array $fileList = _FileListToArray($sourceFolder, "*", 1) ; 2 = folders! 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 ;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 Where exactly would I put the folder path? Sorry, this is my first time using this program. Ex: I want to remove folders that are located here: H:\Exchange\Exchange1 I have a folders within Exchange1 called IMG000623, 000624, 000625 and so on. I want to be able to remove the folders that are seven day's old. Also, does #include <Date.au3>, #include <File.au3> mean that I have to include those into the script as well? Link to comment Share on other sites More sharing options...
rajeshontheweb Posted March 17, 2009 Share Posted March 17, 2009 1) $sourceFolder = @ScriptDir & '\' will read as $sourcefolder = "H:\Exchange\Exchange1\" 2) No u dont need to include those files, Autoit will include those files into the script. let the script editor worry about it when u mention u are new (i am not a good autoit programmer yet, btw) would suggest u use scite4autoit from home page which makes new programmer's life much much easier with syntax highlighting to calltips, etc. Started late is much better than having never started it!!!!Failure is another step towards success. I've been messing around with: Adding Entry to 'Hosts'File Information Lister (Logger)Yet Another AutoIT Error Handler Yet Another AutoIT Error Handler & Debugger Control your App's TaskBar Button YCurrency Ticker (Latest Release : 16 Apr 2009)_WinInetInternetCheckConnection UDF Symantec Definitions Lister UDF _GetLocalIPAddresses UDF UDF to get Special Folder Information WMI_NetworkAdapterConfiguration2Array WMI_CDRomDriveCapabilities _ScriptExists - Check if your au3 script is running!! Uninstaller UDF Get Version for your application (at script level or compiled stage) Uninstaller Pro - faster alternative to windows application removal applet Link to comment Share on other sites More sharing options...
DaRam Posted March 17, 2009 Share Posted March 17, 2009 Just to clarify, $sourceFolder = @ScriptDir & '\' will be "H:\Exchange\Exchange1\"Only if the script is run from H:\Exchange\Exchange1\You can use a $sourceFolde = FileSelectFolder("Choose a folder.", @ScriptDir)to make it interactive.1) $sourceFolder = @ScriptDir & '\'will read as $sourcefolder = "H:\Exchange\Exchange1\"2) No u dont need to include those files, Autoit will include those files into the script. let the script editor worry about itwhen u mention u are new (i am not a good autoit programmer yet, btw) would suggest u use scite4autoit from home page which makes new programmer's life much much easier with syntax highlighting to calltips, etc. Link to comment Share on other sites More sharing options...
Jaymac Posted March 18, 2009 Author Share Posted March 18, 2009 What exactly are these lines telling me? $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 I got it to remove files and not folders as well $fileList = _FileListToArray($sourceFolder, "*", 1) I changed the 1 to a 2 Please help. thanks. apperciate all the help Link to comment Share on other sites More sharing options...
Jaymac Posted March 18, 2009 Author Share Posted March 18, 2009 What information do I change here: ("%s/%s/%s %s:%s:%s", Link to comment Share on other sites More sharing options...
Jaymac Posted March 19, 2009 Author Share Posted March 19, 2009 Can anyone please offer some more help? Link to comment Share on other sites More sharing options...
Mat Posted March 19, 2009 Share Posted March 19, 2009 (edited) from the helpfile format control = The format and flags to use Escape characters can be contain in the "format control" such as \n (@LF), \r (@CR), \t (@TAB]. So if you want to have a "\" you need to use \\, samething for "%" %%. you'd end up with $Date[0]/$Date[1]/$Date[2] $Date[3]:$Date[4]:$Date[5] or 19/03/2009 16:47:33 Hope that explains it... if not look in the help file. search stringformat and its the second one down (pretty self explanitary) MDiesel edit: in answer, don't change anything Edited March 19, 2009 by mdiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 19, 2009 Share Posted March 19, 2009 Where ist your problem? The script is pretty much self-explanatory, isn't it? It is documented. (Okay older should be younger) 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 More sharing options...
Jaymac Posted March 19, 2009 Author Share Posted March 19, 2009 Where ist your problem? The script is pretty much self-explanatory, isn't it? It is documented. (Okay older should be younger)$fileList = _FileListToArray($sourceFolder, "*", 1) ; 2 = folders! (2 doesn't actually remove the folders for me)Also, If _DateDiff('d', $fDate, _NowCalc()) < 7 Then ; the time (is still just removes files (not folders) and it removes the files modified from today.Sorry, I do networking and servers - i'm a newb to coding Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 20, 2009 Share Posted March 20, 2009 $fileList = _FileListToArray($sourceFolder, "*", 1) ; 2 = folders! (2 doesn't actually remove the folders for me)Also, If _DateDiff('d', $fDate, _NowCalc()) < 7 Then ; the time (is still just removes files (not folders) and it removes the files modified from today.Sorry, I do networking and servers - i'm a newb to codingHi,so you want to remove all files and folders in a folder which are older than 7 days, right? 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 More sharing options...
Jaymac Posted March 20, 2009 Author Share Posted March 20, 2009 Hi,so you want to remove all files and folders in a folder which are older than 7 days, right? MegaYes that is exactly what I want to do. Thank you. Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 20, 2009 Share Posted March 20, 2009 Try this: #include <Array.au3> #include <Date.au3> #include <File.au3> ;Root folder $sourceFolder = @ScriptDir & '\Test\' ;Gather files into an array $fileList = _FileListToArray($sourceFolder, "*", 0) ; 0 = files and folders Dim $found[1] ;Loop through array For $i = 1 To $fileList[0] ;Retrieve creation time of file $Date = FileGetTime($sourceFolder & "\" & $fileList[$i], 0, 0) ; 0 modified, 1 = created ;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 If StringInStr(FileGetAttrib($sourceFolder & $fileList[$i]), 'D') Then DirRemove($sourceFolder, 1) Else FileDelete($sourceFolder & "\" & $fileList[$i]) EndIf _ArrayAdd($found, $sourceFolder & $fileList[$i]) ConsoleWrite($sourceFolder & $fileList[$i] & ' deleted' & @CRLF) Else ConsoleWrite('!' & $sourceFolder & $fileList[$i] & @CRLF) EndIf Next $found[0] = UBound($found) _ArrayDisplay($found, 'Deleted items') 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 More sharing options...
sksbir Posted March 20, 2009 Share Posted March 20, 2009 (edited) Hi,so you want to remove all files and folders in a folder which are older than 7 days, right? MegaYou should take care about that. The modification times of a folder reflects the time a file was added or removed within it.So a folder can be older than one of the file which it contains.A other point : a file can be recently created, and modified since a longer time ( copy an old file in a new folder, you will have a new creation date, but the same old modification date ).You didn't said if you want recursion or not, but with recursion, the problem becomes:- 1/ remove all files older than 7 days ( creation and/or modification date ? better is to take the 2 dates in account)- 2/ remove all folders which became empty in 1.@xenobiologist : you should take care about return code of FileGetTime : sometimes, file haven't time at all, and function returns an error. ( some files in temporary internet files for example ) Edited March 20, 2009 by sksbir Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 20, 2009 Share Posted March 20, 2009 HI, it was just an example oh how it could be done. Error checking and check whether it is exactly what the thread owner wanted is another task. (by him) 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now