Link to home
Start Free TrialLog in
Avatar of DGWOLF
DGWOLFFlag for Spain

asked on

Deleting old files except four.

Hello,

I wan to erase files older than 30 days except four in my windows 2003 server. I find the following script in other post, but i don't now how to modify it for do file exceptions.

Const ForAppending = 8
strSourceFolder = "C:\Testing"
strLog = "C:\FilesRemoved.Log"
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOutputFile : Set objOutputFile = objFSO.CreateTextFile(Replace(strLog, ".Log", Month(Date) & Day(Date) & Year(Date) & "_.Log"))
 
With objOutputFile
  .WriteLine "==========================="
  .WriteLine "Removal summary for " & Date
  .WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-=-="
  .WriteLine
End With
 
'intDel is the number of days old you want to check for.  I set it to 1 for testing.
intDel = 30
dtOld = DateAdd("d", -intDel, Date)
 
Set objFolders = objFSO.GetFolder(strSourceFolder)
'Get the files in the current folder
 
For Each fil In objFolders.Files
  'If a file exists that hasn't been accessed in intDel days or more, delete it
  If DateDiff("d", fil.DateCreated, dtOld) >= intDel Then
    objOutputFile.WriteLine fil.Path & " was removed at " & Now
    objFSO.DeleteFile fil.Path
  End If
Next
 
With objOutputFile
  .WriteLine
  .WriteLine "Process completed at " & Now
  .WriteLine "==========================="
  .Close
End With
 
Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit

Thanks for your help.
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

What are the exceptions that make you want to keep those four files? Or, are you saying you want to remove
all but four files that are older than 30 days?
Avatar of DGWOLF

ASKER

It's a backup folder. I do a diferential backup everyday, and i want to remove all old backup but not the firts backup done, because it's necessary to restore.

Thanks.
OK, let me work on it. Not used to windows scripting. we'll have to sort the filelist by date first and then do the deletions that way.
Avatar of DGWOLF

ASKER

Anybody can't help me?
Sorry, forgot about this one. Let me work on it some today.
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh, I forgot to check for the four, you do it in the do while loop and delete the first four from the recordset, then when you search for them in the for each at the bottom, it will not delete those four you deleted from the recordset.