?
Solved

Script to delete files older than #

Posted on 2011-05-10
12
Medium Priority
?
819 Views
Last Modified: 2012-05-11
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!
0
Comment
Question by:JFTech
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35734674
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35735336
Or I have a short VBScript version here:

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

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35735344
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:ReneGe
ID: 35736742
Rough idea:
Convert date to Julian
Convert file date to Julian
IF JulianFileDate LEQ JulianDate - 5 DEL...
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35736777
Another rough idea:
FOR /F "delims=" %%A in ('Robocopy /maxage:5 /L C:\AcronisFolder') DO DEL %%~fA
0
 

Author Comment

by:JFTech
ID: 35742274
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 35742309
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 375 total points
ID: 35742469
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
 
LVL 2

Expert Comment

by:hari_b85
ID: 35800389
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
 

Author Closing Comment

by:JFTech
ID: 35881545
I still have an incomplete working script.
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35881694
@JFTech

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

Do you need further help on this?

Cheers,
Rene
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35883004
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
We all know how boring and exhausting it is to transfer huge web projects developed locally to a webserver simply via FTP. The File Transfer Protocol is a really nice solution if you need to transfer small amounts of files, but if you're plannin…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question