tomzardoz Posted January 24, 2019 Posted January 24, 2019 Hello Friends, Please assist me with the script below. ScitTE SyntaxCheckProd finds no errors. However, I cannot make it find or delete any files. I am running Autoit 3.3.14.5 on Windows 10 Home X64. I have searched the forums and internet for days and can't find anything superior to my present script. My goal is to: 1. Delete folders and files recursively older than 15 days. 2. No GUI needed. 3. Do not force deletion and no confirmation needed. 4. Don't want to use forfiles. Autoit Script: #RequireAdmin #include <Date.au3> #include <File.au3> const $MAXAGE = 3 ; days global $msg = "" global $nFilesDel = 0 $retval = _FileDeleteRecursive("C:\test","*.*",$MAXAGE) func _FileDeleteRecursive($sPath, $pattern, $maxage) local $files = _FileListToArray($sPath,$pattern,1) if @error <> 0 then return -1 local $dirs = _FileListToArray($sPath,"*",2) if @error <> 0 then return -1 local $age, $nFilesFound = 0, $retval if IsArray($files) then for $n = 1 to $files[0] $age = _DateDiffInDays($sPath & "\" & $files[$n]) if $age > $maxage then _DeleteFile($sPath & "\" & $files[$n],$age) $nFilesFound += 1 endif next endif if IsArray($dirs) then for $n = 1 to $dirs[0] $retval = _FileDeleteRecursive($sPath & "\" & $dirs[$n], $pattern,$maxage) if $retval > 0 then $nFilesFound += $retval next endif return $nFilesFound endfunc func _DateDiffInDays($filename) if not FileExists($filename) then return -1 local $filetime = FileGetTime($filename) local $tempDate = $filetime[0] & "/" & $filetime[1] & "/" & $filetime[2] local $currDate = @YEAR & "/" & @MON & "/" & @MDAY return _DateDiff("D",$tempDate,$currDate) endfunc func _DeleteFile($filepath,$age) $msg = $msg & "Age: " & $age & " File: " & $filepath & @CRLF FileDelete($filepath) $nFilesDel += 1 endfunc
BrewManNH Posted January 24, 2019 Posted January 24, 2019 Instead of FileListToArray, you could use _FileListToArrayRec() which will give you the list of files in the folder, and all subfolders. Then you can delete the ones that are over your target date. Next, your problem is probably this line: If @error <> 0 Then Return -1 If your C:\Test folder is empty of files, then it will immediately return, and the script ends. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
xcaliber13 Posted January 24, 2019 Posted January 24, 2019 Maybe something like this? Local $sYDate = StringRegExpReplace(_DateAdd("D", -15, _NowCalcDate()), '\d\d(\d\d).(\d\d).(\d\d)', '$01$2$3') ; Format = YYMMDD Local $sDate = "20"&$sYDate $selectedfolder = "C:\test" $aFileList = _FileListToArray($selectedfolder, "*", $FLTA_FILES) Sleep(5000) For $i = 1 To $aFileList[0] Local $sArray = FileGetTime("C:\test\"& $aFileList[$i], $FT_MODIFIED, 1) If $sArray < $sDate Then FileDelete("C:\test\"& $aFileList[$i]) EndIF Next
tomzardoz Posted February 7, 2019 Author Posted February 7, 2019 Hello BrewManNH, thank you for your suggestion. I followed it and it now works well. Many thanks! Tom
tomzardoz Posted February 7, 2019 Author Posted February 7, 2019 Hello, xcaliber13, I tried your suggestion and got several syntax errors which I have attached below. WIll you kindly look this over and tell me what I can do to remedy it?
MetaSteel Posted February 7, 2019 Posted February 7, 2019 tomzardoz, you still need the includes at the top #include <Date.au3> #include <File.au3>
tomzardoz Posted February 8, 2019 Author Posted February 8, 2019 Hello MetaSteel, thank you, your suggestion cleared the syntax errors. May I ask another question? How to make this script recursive in all sub directories? See script below. Thanks! Script: #include <Date.au3> #include <File.au3> Local $sYDate = StringRegExpReplace(_DateAdd("D", -15, _NowCalcDate()), '\d\d(\d\d).(\d\d).(\d\d)', '$01$2$3') ; Format = YYMMDD Local $sDate = "20"&$sYDate $selectedfolder = "C:\test" $aFileList = _FileListToArray($selectedfolder, "*", $FLTA_FILES) Sleep(5000) For $i = 1 To $aFileList[0] Local $sArray = FileGetTime("C:\test\"& $aFileList[$i], $FT_MODIFIED, 1) If $sArray < $sDate Then FileDelete("C:\test\"& $aFileList[$i]) EndIF Next
BrewManNH Posted February 8, 2019 Posted February 8, 2019 Look up a couple of posts, I told you how to do that. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
tomzardoz Posted February 8, 2019 Author Posted February 8, 2019 Hi BrewManNH, I did as you suggested and that cleared the syntax errors however, that did not delete the old files recursively. How can this be made to work now? Here is the modified script: #include <Date.au3> #include <File.au3> Local $sYDate = StringRegExpReplace(_DateAdd("D", -15, _NowCalcDate()), '\d\d(\d\d).(\d\d).(\d\d)', '$01$2$3') ; Format = YYMMDD Local $sDate = "20"&$sYDate $selectedfolder = "C:\test" $aFileList = _FileListToArrayRec ($selectedfolder, "*", $FLTA_FILES) Sleep(5000) For $i = 1 To $aFileList[0] Local $sArray = FileGetTime("C:\test\"& $aFileList[$i], $FT_MODIFIED, 1) If $sArray < $sDate Then FileDelete("C:\test\"& $aFileList[$i]) EndIF Next Thanks for your continued help! Tom
MetaSteel Posted February 8, 2019 Posted February 8, 2019 (edited) Tom, I tried that code and it worked just fine. Perhaps you don't have any files that are older than 15 days on your test folder. Or perhaps it needed the admin privileges you originally had in your script. Feel free to try this version: #RequireAdmin #include <Date.au3> #include <File.au3> Local $sSelectedFolder = 'C:\temp' Local $iCutOffDays = 15 Local $sCutOffDate = StringRegExpReplace(_DateAdd('D', -$iCutOffDays, _NowCalcDate()), '(\d\d\d\d).(\d\d).(\d\d)', '$01$2$3') Local $aFileList = _FileListToArrayRec($sSelectedFolder, '*', $FLTA_FILES) For $i = 1 To $aFileList[0] Local $sStatus = ' REMAINS' Local $sFileName = $sSelectedFolder & '\' & $aFileList[$i] Local $sFileDate = FileGetTime($sFileName, $FT_MODIFIED, $FT_STRING) ConsoleWrite($sFileName) If StringCompare($sFileDate, $sCutOffDate) < 0 Then If FileDelete($sFileName) Then $sStatus = ' DELETED' EndIF ConsoleWrite($sStatus & @CRLF) Next Note that it's not all that different than: #pragma compile(Console, true) #RequireAdmin $sSelectedFolder = "C:\test" $iCutOffDays = 15 RunWait('forfiles -p "." -s -m *.* -d -' & $iCutOffDays & ' -c "cmd /c del @path"', $sSelectedFolder) Edited February 8, 2019 by MetaSteel
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