DGWOLF
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.Fi leSystemOb ject")
Dim objOutputFile : Set objOutputFile = objFSO.CreateTextFile(Repl ace(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(strSource Folder)
'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.
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.Fi
Dim objOutputFile : Set objOutputFile = objFSO.CreateTextFile(Repl
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(strSource
'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.
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.
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.
ASKER
Anybody can't help me?
Sorry, forgot about this one. Let me work on it some today.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
all but four files that are older than 30 days?