Script to delete files older than #


I am looking for a simple batch file or another solution to automatically delete some backup files that are generated by acronis software to a file server. I already found this artical on experts exchange:


However the two best solutions dont work for me...

This script if the echo is left in prints out the files older than 5 days. But once the echo statement is removed just errors out:

C:\test>"New Text Document.bat"

C:\test>forfiles -pc:\test -s -d-5 -c"CMD /C if @ISDIR==FALSE echo DEL\"@PATH\@F

C:\test>"New Text Document.bat"

C:\test>forfiles -pc:\test -s -d-5 -c"CMD /C if @ISDIR==FALSE DEL\"@PATH\@FILE\"
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.

This script kills everything in the folder including the script itself:

@echo off
echo wscript.echo DateAdd("d",-5,date)>"5daysago.vbs"

for /f "tokens=2-4 delims=/ " %%a in ('cscript //nologo "5daysago.vbs"') do (
   for /f "tokens=*" %%A in ('dir /a-d /b *.*') do (
      for /f "tokens=2-4 delims=/ " %%d in ('echo %%~tA') do (
         if %%f%%d%%e leq %%c%%a%%b del "%%A"

I am open to other solutions to! Help!
Who is Participating?
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Not sure what you are trying there.  I assume you were looking at this one?  If so save it as delold.vbs say and then run it using Cscript //nologo delold.vbs or double click in explorer if you prefer.  As it stands it deletes files over 1 month old from c:\temp.  Change that to "c:\test" as you wish and the DateAdd command to "d" and "-5" for 5 days old.

If you don't want it to go down subdirs you can remove the whole second "For... Next" loop section.

The other script works on a DIR listing and can be a .BAT or .CMD file.


' Call initial folder to delete and it will work down subdirs
' By running recursively through subdirs after doing files
' in each dir. Second parameter is date of last files to leave
' Anything older will go.
' Stephen Knight July 2009

Dim deletionDate
Dim fso
Dim oFile
Dim oFolder

Set fso = CreateObject("Scripting.FileSystemObject")

DeleteFiles "c:\temp",DateAdd("m", -1, Date)

msgbox "Deleting older than " & DateAdd("m", -1, Date)

Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

Function DeleteFiles(foldername,cutoffdate)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
If ofile.DateCreated < cutoffDate Then
fso.DeleteFile oFile, True
End If

For Each subFolder In oFolder.SubFolders
DeleteFiles subFolder.Path, cutoffdate
If (SubFolder.Files.Count = 0) and (SubFolder.SubFolders.Count=0) And (SubFolder.DateCreated < cutoffdate) Then
msgbox "deleting " & & " created on " & subfolder.DateCreated
call SubFolder.Delete()
End If

End Function

Bill PrewCommented:
If you don't mind using a small free command line utility, take a look at DELAGE32:

Steve KnightIT ConsultancyCommented:
Or I have a short VBScript version here:

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Steve KnightIT ConsultancyCommented:
If you want pure batch you could also just keep the newest 5 files say:
Rough idea:
Convert date to Julian
Convert file date to Julian
IF JulianFileDate LEQ JulianDate - 5 DEL...
Another rough idea:
FOR /F "delims=" %%A in ('Robocopy /maxage:5 /L C:\AcronisFolder') DO DEL %%~fA
JFTechAuthor Commented:
Script on dragon IT seems to bomb out:

Windows Script Host Error:

Line 1
Char 1
Error Type mismatch: ;DeleteFiles'
Code: 800A000D
Source: Ms VBS runtime error
That's because you are tring to run it as a VBScript.
Since it's a batch file, give it a ".bat" extension. Should work fine!
A simple way might be moving the older files to a new folder and then deleting them, for example,
if i have files on \\filesvr\backupdata
@Echo off
md \\filesvr\backupdata\olderdata

robocopy "\\filesvr\backupdata" "\\filesvr\backupdata\olderdata" /e /v /r:2 /w:0 /MINAGE:5 /mov
RD /S /Q "\\filesvr\backupdata\olderdata"
JFTechAuthor Commented:
I still have an incomplete working script.

If your script is incomplete, why did you close the question?

Do you need further help on this?

Steve KnightIT ConsultancyCommented:
Yes you waited 3 weeks with no feedback and then awarded a "B" grade... if you offer constructive feedback then we can continue helping.  With no feedback in general you get no more comments awaiting some feedback one way or the other....

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.