copyleft Posted April 2, 2023 Posted April 2, 2023 Tryin to delete files older than 21 days in a network folder. The user has admin access to the folder, but the script, below doesn't delete any files. #RequireAdmin #include <File.au3> #include <Date.au3> Global $sFolderPath = "\\SHARE\home\Logs" Global $iDaysToKeep = 21 Global $sDate = _DateAdd('d', -$iDaysToKeep, _NowCalcDate()) For $sFile In _FileListToArray($sFolderPath) $sFilePath = $sFolderPath & "\" & $sFile $sFileDate = FileGetTime($sFilePath, 1) If $sFileDate <> -1 And $sFileDate < $sDate Then FileDelete($sFilePath) EndIf Next
Danp2 Posted April 2, 2023 Posted April 2, 2023 FileGetTime returns an array by default. You can change this behavior by supplying a value for the optional format parameter. $sFileDate = FileGetTime($sFilePath, 1, 1) ; $FT_CREATED, $FT_STRING Latest Webdriver UDF Release Webdriver Wiki FAQs
Moderators Melba23 Posted April 2, 2023 Moderators Posted April 2, 2023 Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states: Quote General development and scripting discussions. Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums. Expand Moderation Team Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Reveal hidden contents ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
copyleft Posted April 2, 2023 Author Posted April 2, 2023 @Danp2 I'm lost here. Do you mean supply a path value for $sFilePath like below? #RequireAdmin #include <File.au3> #include <Date.au3> Global $sFolderPath = "\\SHARE\home\Logs" Global $sfilepath = "\\SHARE\home\Logs\*.htm" Global $iDaysToKeep = 21 Global $sDate = _DateAdd('d', -$iDaysToKeep, _NowCalcDate()) For $sFile In _FileListToArray($sFolderPath) $sFilePath = $sFolderPath & "\" & $sFile $sFileDate = FileGetTime($sFilePath, 1, 1) ; $FT_CREATED, $FT_STRING If $sFileDate <> -1 And $sFileDate < $sDate Then FileDelete($sFilePath) EndIf Next
Danp2 Posted April 2, 2023 Posted April 2, 2023 No, I posted the corrected line of code above, which you have incorporated into your code AFAICS. Your issue now is likely that the string format from _DateAdd isn't the same as that from FileGetTime. Have you checked the forum? I'm pretty sure this scenario has already been discussed previously. Latest Webdriver UDF Release Webdriver Wiki FAQs
copyleft Posted April 2, 2023 Author Posted April 2, 2023 (edited) @Danp2 I have searched the forums. I think something else is broke because trying do delete files older than X doesn't even work with a batch script, which is what brought me here in the first place. Edited April 2, 2023 by copyleft
Danp2 Posted April 2, 2023 Posted April 2, 2023 IDK. You should add some ConsoleWrites so that you can observe the values of the strings being compared. You could also add error checking to see if it's a rights issue, etc. Latest Webdriver UDF Release Webdriver Wiki FAQs
copyleft Posted April 2, 2023 Author Posted April 2, 2023 Added network path check and console write; script completes with no errors but still no files deleted, like I said, must be some other Windows issue. I'll move along unless someone has other ideas. #RequireAdmin #include <File.au3> #include <Date.au3> Global $sNetworkPath = "\\SHARE\home\Logs" If FileExists($sNetworkPath) Then ConsoleWrite("Network path is accessible." & @CRLF) Global $sCurrentDate = _NowCalc() Global $sOldDate = _DateAdd('d', -6, $sCurrentDate) Global $aFileList = _FileListToArray($sNetworkPath, "*.htm", 1) For $i = 1 To $aFileList Global $sFileDate = FileGetTime($sNetworkPath & "\" & $aFileList[$i], 1, 1) If $sFileDate <> -1 And $sFileDate < $sOldDate Then FileDelete($sNetworkPath & "\" & $aFileList[$i]) ConsoleWrite("Deleted file: " & $aFileList[$i] & @CRLF) EndIf Next Else ConsoleWrite("Network path is not accessible." & @CRLF) EndIf If $CmdLine > 0 And $CmdLine = "/console" Then ConsoleWrite("Running script in console mode..." & @CRLF) Else ConsoleWrite("Running script in normal mode..." & @CRLF) EndIf
Danp2 Posted April 2, 2023 Posted April 2, 2023 Your string comparisons are off if you never get the ConsoleWrite regarding deleted files. Latest Webdriver UDF Release Webdriver Wiki FAQs
rudi Posted April 6, 2023 Posted April 6, 2023 (edited) Hello, where it's convenient, I like to use powershell from within Autoit. You could invoke a powershell one-liner with no need to do any loop testing: $LogDir="\\servername\sharename\log" $Params='-noprofile -executionpolicy bypass -command "gci "' & $LogDir & '" -file -force -ea si| ? lastwritetime -lt $(get-date).adddays(-21) | rm -force -ea si' ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Params = ' & $Params & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ShellExecute("powershell",$Params) Notes: gci -> get-childitem -ea si -> erroraction silentlycontinue - suppress any error messages ? -> where - testing condition -lt -> less than (older than) rm -> remove-item Edited April 6, 2023 by rudi Earth is flat, pigs can fly, and Nuclear Power is SAFE!
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