#133419 - 2005-02-07 02:52 PM
Delete all files exept the latest?
|
Finbom
Fresh Scripter
Registered: 2003-06-27
Posts: 21
|
Hello! I have a folder that stores some files for backup. I am trying to write a kixscript that would delete all files exept the last 10. Can anyone help me out? I checked out the UDF cleandirectory() but that udf does the reverse,, delete whats older, and by date.
Regards, Magnus Finbom
|
|
Top
|
|
|
|
#133420 - 2005-02-07 10:22 PM
Re: Delete all files exept the latest?
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
You should be able to get some ideas from this:
Requires: MAILER() LOGGER() DATECALC() WSHPIPE()
Code:
CLS BREAK ON $log=@scriptdir+'\Profileremove.csv' IF EXIST($log) DEL $log ENDIF $date=split(@date,'/')[1]+'/'+split(@date,'/')[2]+'/'+split(@date,'/')[0] $logdata='Run date/time:'+$date+' '+@time+@crlf $logdata=$logdata+'Days ago,Location,Error Code'+@crlf+'-------------------' LOGGER($log,$logdata+@crlf)
FOR EACH $i IN Split('01 02 03') $loc='\\citrixserver'+$i+'\admin$$\profiles\' $rc=WshPipe('%comspec% /c dir '+$loc+'*.*',1) FOR EACH $line IN $rc IF NOT instr($line, 'File Not Found') ;?$line ; -- Convert to American Dates from European format ;?$dtchk $name=split($line)[17] $dtchk=split(split($line)[0],'/')[2]+'/'+split(split($line)[0],'/')[0]+'/'+split(split($line)[0],'/')[1] $dt=DATECALC($dtchk,@date) IF val($dt)>2 AND len($name)>3 AND $name<>'All Users' AND $name<>'DEFAULT' ;IF len($name)>3 AND $name<>'All Users' AND $name<>'DEFAULT' SHELL '%COMSPEC% /C RD /S /Q '+chr(34)+$loc+'\'+$name+chr(34) If @error<>$err=1 ELSE $err=0 ENDIF $logdata=''+$dt+','+$loc+$name+','+$err LOGGER($log,$logdata+@crlf) ENDIF ENDIF NEXT NEXT MAILER('user1@@company.com,user2@@company.com',,,$log)
Thanks,
Kent
|
|
Top
|
|
|
|
#133422 - 2005-02-07 11:16 PM
Re: Delete all files exept the latest?
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
I assume you mean delete all fiels except that last 10 days worth? needs DirPlus(), FlipCTime(), and TimeConvert()
Code:
$folder = "%temp%" for each $file in dirplus($folder,"/a-d")
$Filedate = split($file.datelastmodified) $filedate[1] = timeconvert($filedate[1]+" "+ $filedate[2])
$filedate[0] = split($filedate[0],"/")
$filedate = flipctime($filedate[0][2]+"/"+$filedate[0][0]+"/"+$filedate[0][1],$filedate[1])
if $filedate <= flipctime(@date,@time) - 864000 ;the number of seconds for 10 days ? "10+ days old " " " $file.name " " $file.datelastmodified endif next
|
|
Top
|
|
|
|
#133424 - 2005-02-08 10:48 AM
Re: Delete all files exept the latest?
|
Finbom
Fresh Scripter
Registered: 2003-06-27
Posts: 21
|
Hi!
Sorry if I was unclear about the goal.
Well, I have a folder that stores a copy of a certain users registry key for an application. And the software generates a new file each day. I want to keep the 10 lateast files. and IF there are more then 10 files the oldest should be erased first. I think some UDF's could to this together, but I really cant solve actually how to do it.
Regards, Magnus Finbom
|
|
Top
|
|
|
|
#133425 - 2005-02-08 01:20 PM
Re: Delete all files exept the latest?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Code:
Break On
Go C:
cd "\temp"
$iLimit=10
$sResult=udfPipe('"'+%COMSPEC%+'" /C dir /a-d /o-d /b')
If @ERROR
"Could not get files. Error is:" ?
"["+@ERROR+"] "+@SERROR ?
"Extended error: " ?
$sResult[1] ?
Else
For Each $sFile in Split($sResult[0],@CRLF)
If $sFile
If $iLimit
"Will not delete file "+$sFile+@CRLF
$iLimit=$iLimit-1
Else
"Will delete file "+$sFile+@CRLF
EndIf
EndIf
Next
EndIf
; 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=82578
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 ;}}}
|
|
Top
|
|
|
|
#133426 - 2005-02-08 02:51 PM
Re: Delete all files exept the latest?
|
Finbom
Fresh Scripter
Registered: 2003-06-27
Posts: 21
|
Wow, You rocks! 
Always fast answers with solutions. I will try this asap.
Thanks!
|
|
Top
|
|
|
|
#133428 - 2005-02-09 09:36 AM
Re: Delete all files exept the latest?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
If one file will be created each day then Cleandirectory with the optional parameter of deleting only files older than ten days would definitely fit the bill. It's all documented in the UDF header.
Not really.
Introducing a date based selection changes the action from "keep the last 10 files" to "keep the files that are no more than 10 days old" and is the sort of thing that will bite you unexpectedly.
Say you are on vacation for a week-and-a-bit. When you return to work the clean-up routine will delete all your backup files because they will be over the 10 day limit.
Without knowing what the appliaction is and how it runs we can't be sure that it's a problem in this particular case, but it certainly would be a problem in many cases.
|
|
Top
|
|
|
|
#133429 - 2005-02-09 12:06 PM
Re: Delete all files exept the latest?
|
Finbom
Fresh Scripter
Registered: 2003-06-27
Posts: 21
|
Hi again!
Each day the user log of the system a certain registry key is exported to a reg-file. This regkey contain info for an application that all our users uses, and we want to make sure that we can restore this key when deleting the users profile in case of problem.
The last script above did what I wanted perfectly. It always keeps at least 10 files and when exeding 10 the last is deleted first. Making sure that even if the user is away for long time, the last files are saved.
One more question though...  Beside the regkey I also backup a certain file on the computer... And just as above I only want to save the last 10 and only 10 files. No problem, I thought. I took the code and inserted it once more in the script to run on another folder. But NO. It deletes tha newest file and keeps older ones.
What I have done is to simply insert the code two times after each other in the script.. Have I missed something or done wrong?
Regards, Magnus Finbom
Code:
Go H: CD "\egnadata\Backup\CIC" $iLimit=10 $sResult=udfPipe('"'+%COMSPEC%+'" /C dir /a-d /o-d /b') If @ERROR "Could not get files. Error is:" ? "["+@ERROR+"] "+@SERROR ? "Extended error: " ? $sResult[1] ? Else For Each $sFile In Split($sResult[0],@CRLF) If $sFile If $iLimit "Will not delete file "+$sFile+@CRLF $iLimit=$iLimit-1 Else "Will delete file "+$sFile+@CRLF Del $sFile EndIf EndIf Next EndIf
Go H: CD "\egnadata\Backup\Outlook" $iLimit=10 $sResult=udfPipe('"'+%COMSPEC%+'" /C dir /a-d /o-d /b') If @ERROR "Could not get files. Error is:" ? "["+@ERROR+"] "+@SERROR ? "Extended error: " ? $sResult[1] ? Else For Each $sFile In Split($sResult[0],@CRLF) If $sFile If $iLimit "Will not delete file "+$sFile+@CRLF $iLimit=$iLimit-1 Else "Will delete file "+$sFile+@CRLF Del $sFile EndIf EndIf Next EndIf ; 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 ;}}}
|
|
Top
|
|
|
|
#133430 - 2005-02-09 01:00 PM
Re: Delete all files exept the latest?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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.
|
|
Top
|
|
|
|
#133431 - 2005-02-09 02:05 PM
Re: Delete all files exept the latest?
|
Finbom
Fresh Scripter
Registered: 2003-06-27
Posts: 21
|
A big thank's! Soo cool.
It works like a charm. Regardsing the other files it was the timestamp that is the prolem. It never updates. But thats another issue.
Once again. Thank You!
Regards, Magnus Finbom
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|