Need a Batch Script that can Delete Files Older Than a certain Date (On Windows)

I checked different Batch Scripts, but none worked for me...
The Batch Script should delete Files from a certain Directory.
It should read the current Date, and delete all the files older than this date. Ex: 2 Days older.
One more thing: Can I make it run automatically as windows starts ?
jmhabis2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
Have a look at this to start with:

'
' Script to delete all files in a folder beyond a certain date
'
Dim deletionDate
Dim fso
Dim oFile
Dim oFolder
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder("c:\your folder")
 
deletionDate = Date() -2
 
 
For Each oFile in oFolder.Files
    If DateDiff("d", oFile.DateCreated, deletionDate) > 0 Then
        ' File exceeds the date selected, delete it
        fso.DeleteFile oFile, True ' Delete it even if it is read only!
    End If
Next
 
Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

save it as delolder.vbs and amend the folder / date entry.  You can run it from a shorcut in startup dir say using

cscript //nologo c:\subdir\delolder.vbs

hth

Steve
0
Steve KnightIT ConsultancyCommented:
If you want subdrs of that folder too then try:

'
' Script to delete all files in a folder beyond a certain date
'
Dim deletionDate
Dim fso
Dim oFile
Dim oFolder

 
Set fso = CreateObject("Scripting.FileSystemObject")

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

DeleteFiles  "c:\folder",DateAdd("d", -2, 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.DateLastModified < cutoffDate Then
        fso.DeleteFile oFile, True
    End If
Next
 
For Each subFolder In oFolder.SubFolders
    DeleteFiles subFolder.Path, cutoffdate
Next
End Function
0
merowingerCommented:
This vbscript (.vbs) should do it
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objFolder = objFSO.GetFolder("D:\Privat")

For Each file In objFolder.Files

	If objFSO.GetFile(file).DateLastModified < Now -2 Then
		objFSO.DeleteFile file,True
	End If
Next

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

merowingerCommented:
For automatic start with Win Vista and Win 7 you can use Task Scheduler (see picture) .
Or you can use a policy (gpedit.msc)
Computer Configuration -> Windows Settings -> Scripts -> Startup
task.jpg
0
jmhabis2Author Commented:
Dragon-IT:
Hi,

DeleteFiles  "c:\folder",DateAdd("d", -2, Date) is the directory name is all that I have to change?
Is there anything else in this script that I should change depending on what I have on my computer
0
Steve KnightIT ConsultancyCommented:
Nope thats it, the path, 2 is numbr of days, "d" is days (or use "m" for months)

Steve
0
jmhabis2Author Commented:
Merowinger I need a script that runs as .bat.(Batch Script)
0
Steve KnightIT ConsultancyCommented:
a true batch file is doable but massive.  you can jalways run a vbscript from a batch file if needed.

out at the mo but if you want  pure batch search for some solutions by stevegtr in the msdos area.
0
Bill PrewCommented:
There's a great little freeware utility at the page below called DELAGE32 that can do what you want with no scripting at all.  Just schedule the program to run at system startup with the option you want (folder to process, recurse if subfolders should be scanned, number of days old to purge, etc) and it will do the rest.

~bp
0
Bill PrewCommented:
Sorry, forgot to paste the link, here it is.

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

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
The pure batch method I meant is in StveFTR's post here:

http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_21887658.html

but personally I'd use VBscript or an external util like Bill suggested, though the batch file ones are tried and tested too...
0
Justin EllenbeckerIT DirectorCommented:
This should do it just schedule it and fill in the path information if you need to add paths just add the lines and recall teh function.  You posted in the VBS section but you could always save the vbs to the computer and then use a batch file to call the vbs.

csript.exe C:\script.vbs

Simply make that your batch file and save the vbs.
Set objFSO = CreateObject("Scripting.FileSystemObject") 

path1 = "Your Path Here"
FolderChk(path1)

Function FolderChk(dirPath)
	On Error Resume Next
	Set oFolder = objFSO.GetFolder(dirPath)
	For Each file In ofolder.files
		FileDate = Split(CStr(file.DateCreated), " ")
		If FileDate(0) < Date - 2 Then file.delete
	Next
End Function

Open in new window

0
Steve KnightIT ConsultancyCommented:
StrifeJester.... kinda repeats merowinger and my own VBScript solutions (and suggestions as running from batch)....

Steve
0
Steve KnightIT ConsultancyCommented:
Ok.... can I ask why, when you asked for a batch script a VBScript called from batch wasn't acceptable but a third party exe was?  Not that there is anything wrong with that exe, or billprew suggesting it but pleae don't waste our time asking for a script if you don't want one!
0
jmhabis2Author Commented:
I was initially looking for a batch script. however the solutions proposed were not straight forward.
For my knowledge of Windows I felt it was a bit complex (I don't know how to run VBscript from batch)
I ended evaluating this application, that is actually running via a batch script.
I hope I managed to answer your question.


0
merowingerCommented:
just call the following in batch to start a vbscript

cscript.exe yourscript.vbs
0
jmhabis2Author Commented:
thanks a lot
0
Steve KnightIT ConsultancyCommented:
Sorry if it sounded a bit harsh, having a bad day!  We did tell you how to run it from a batch script, and indeed afaik the first post gave you an answer to your question but you've got what you want which is the main thing...

Steve
0
jmhabis2Author Commented:
No worry. you are right.
my mistake
0
Bill PrewCommented:
I hear you Steve, sorry the day isn't going great.  At least your house didn't flood last week ;-).

I often find that I too have to come to terms with the fact that the posters of EE questions don't have to accept the first working solution, but rather get to pick the one that they prefer, and feels the best to them.  I'm had a number of cases where I proposed a viable solution early in a thread only to see a later post be the one selected by the poster.  I remind myself they have that right, but sometimes a little hard to swallow.

I also (like you) often have to come to grips with the fact that posters can, do, and are allowed to change the rules, and reveal new and different information along the path of a posting.  I sometimes don't like it, but it seems to be the nature of the beast here on EE.

Hope your weekend is better...

~bp
0
Steve KnightIT ConsultancyCommented:
Me too... just been with 4 year old to the chippy to get tea so not all bad!!

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.