Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need to delete files older than two days inside a folder, except files last modified the 1st of each month. Forfiles does not support exceptions. Operating System is Windows Server 2003. Any idea?

Thank you all for help!

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Quick and dirty VBScript here, it assumes UK date format for enumerating if the file is from the 1st of the month. If you use a different date format then change the trim command to grab the appropriate number positions. For example, a US date would be Mid(objFile.DateLastModified, 4, 2) = 01 rather than Left(objFile.DateLastModified, 2) = 01

Anyway, sorry for the dirty code... but it should get you started.

strFolder = "C:\DeleteTest"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files
strDaysOld = 2
'Get files from target folder
For Each File in colFiles
set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
'Enumerate last modified date/time and delete if older than 2 days but where the date doesn't start with '01'
If objFile.DateLastModified < (Date() - strDaysOld) AND NOT Left(objFile.DateLastModified, 2) = 01 Then
objFSO.DeleteFile objFile, true
End If
Next
share|improve this answer

I'd say that would be dependent on your familiarity and comfort with programming/scripting languages.

For example, here's a PowerShell example. I'm a bit more partial to Python, Perl, (the *nix/Linux) varieties and something like that would be pretty easy in Python (example).

Both of those examples came from Googling powershell delete files older than or python delete files older than ...in case you wanted to check for alternatives.

I'm not familiar with any GUI tools or quick setup tools to do such, as I've never really needed them. All of what you ask can be coded out in less than 20 lines in most cases.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.