Script to delete files older than #

Hello,

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:

EE

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
ILE\""
DEL"C:\test\support_oracle.JPG"
DEL"C:\test\ThisOne.bmp"
DEL"C:\test\Travis.jpg"
DEL"C:\test\TrendMicro.bmp"
DEL"C:\test\TrendMicro2.bmp"

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!
JFTechAsked:
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.

Steve

' 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
Next

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 " & subfolder.name & " created on " & subfolder.DateCreated
call SubFolder.Delete()
End If
Next

End Function

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

http://home.mnet-online.de/horst.muc/win/delage.htm
http://home.mnet-online.de/horst.muc/wbat32.htm

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

http://scripts.dragon-it.co.uk/links/batch-delete-files-older-than

Steve
0
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:

http://scripts.dragon-it.co.uk/links/batch-versioning-backup
0
 
ReneGeCommented:
Rough idea:
Convert date to Julian
Convert file date to Julian
IF JulianFileDate LEQ JulianDate - 5 DEL...
0
 
ReneGeCommented:
Another rough idea:
FOR /F "delims=" %%A in ('Robocopy /maxage:5 /L C:\AcronisFolder') DO DEL %%~fA
0
 
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
0
 
ReneGeCommented:
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!
0
 
hari_b85Commented:
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
cls
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"
0
 
JFTechAuthor Commented:
I still have an incomplete working script.
0
 
ReneGeCommented:
@JFTech

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

Do you need further help on this?

Cheers,
Rene
0
 
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....


Steve
0
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.