Link to home
Start Free TrialLog in
Avatar of jmhabis2
jmhabis2Flag for Lebanon

asked on

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 ?
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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
Avatar of jmhabis2

ASKER

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
Nope thats it, the path, 2 is numbr of days, "d" is days (or use "m" for months)

Steve
Merowinger I need a script that runs as .bat.(Batch Script)
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.
Avatar of Bill Prew
Bill Prew

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
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The pure batch method I meant is in StveFTR's post here:

https://www.experts-exchange.com/questions/21887658/Batch-file-to-delete-files-older-than-x-days.html

but personally I'd use VBscript or an external util like Bill suggested, though the batch file ones are tried and tested too...
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

StrifeJester.... kinda repeats merowinger and my own VBScript solutions (and suggestions as running from batch)....

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


just call the following in batch to start a vbscript

cscript.exe yourscript.vbs
thanks a lot
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
No worry. you are right.
my mistake
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
Me too... just been with 4 year old to the chippy to get tea so not all bad!!

Steve