Take the 2-minute tour ×

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
add comment

2 Answers

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
share|improve this answer
add comment

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
add comment

Your Answer


