It shouldn't be a problem.
Here is a new version, with the main code as a UDF so you don't have to keep repeating it.
This new version allows wildcards, so you could do "udfPurgeFiles("H:\backup\out*.txt",5) to keep the last 5 files which match "out*.txt" in the H:\backup directory.
Code:
Break On
$sDirToPurge="H:\egnadata\Backup\CIC"
udfPurgeFiles($sDirToPurge,10)
If @ERROR " ERROR: Could not purge '"+$sDirToPurge+"'"+@CRLF+"REASON: " @ERROR " " @SERROR+@CRLF EndIf
$sDirToPurge="H:\egnadata\Backup\Outlook"
udfPurgeFiles($sDirToPurge,10)
If @ERROR " ERROR: Could not purge '"+$sDirToPurge+"'"+@CRLF+"REASON: " @ERROR " " @SERROR+@CRLF EndIf
Exit 0
Function udfPurgeFiles($sPath,$iLimit)
Dim $sResult,$sFile
Dim $sDirPath
$sDirPath=$sPath
If Not (16 & GetFileAttr($sDirPath))
; Not a directory - maybe there is a search term?
$sDirPath=Left($sDirPath,InStrRev($sDirPath,"\")-1)
EndIf
$sResult=udfPipe('"'+%COMSPEC%+'" /C dir /a-d /o-d /b '+$sPath)
If @ERROR Exit @ERROR EndIf
For Each $sFile in Split($sResult[0],@CRLF)
If $sFile
If $iLimit
"Will not delete file "+$sDirPath+"\"+$sFile+@CRLF
$iLimit=$iLimit-1
Else
"Will delete file "+$sDirPath+"\"+$sFile+@CRLF
; *** Uncomment the next line to enable the delete payload ***
; DEL $sDirPath+"\"+$sFile
EndIf
EndIf
Next
Exit 0
EndFunction
; Execute a command and return output and error streams
; This code adapted from Chris S. WshPipe() http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=8257
Function udfPipe($sCommand)
Dim $oExec
Redim $udfPipe[2]
$oExec = CreateObject("WScript.Shell").Exec($sCommand)
If Not VarType($oExec)=9 $udfPipe[1]=@SERROR Exit @ERROR EndIf
$udfPipe[0] = $oExec.StdOut.ReadAll
$udfPipe[1] = $oExec.StdErr.ReadAll
Exit $oExec.ExitCode
EndFunction ;}}}
If it is *still* selecting the wrong files, check the timestamps on them, and see what the DIR command does if you type it on the command line.
PS, I've disabled the "DEL" in the UDF to avoid accidents. Just remove the comment character to activate it.