[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2010-04-09
21
Medium Priority
?
725 Views
Last Modified: 2012-05-09
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 ?
0
Comment
Question by:jmhabis2
  • 9
  • 5
  • 3
  • +2
21 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 30182665
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 30182749
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
 
LVL 31

Expert Comment

by:merowinger
ID: 30182935
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
LVL 31

Expert Comment

by:merowinger
ID: 30183116
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
 

Author Comment

by:jmhabis2
ID: 30186206
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 30186352
Nope thats it, the path, 2 is numbr of days, "d" is days (or use "m" for months)

Steve
0
 

Author Comment

by:jmhabis2
ID: 30186355
Merowinger I need a script that runs as .bat.(Batch Script)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 30188437
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
 
LVL 60

Expert Comment

by:Bill Prew
ID: 30194046
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
 
LVL 60

Accepted Solution

by:
Bill Prew earned 1000 total points
ID: 30194129
Sorry, forgot to paste the link, here it is.

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

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 30200256
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
 
LVL 17

Expert Comment

by:Justin Ellenbecker
ID: 30210385
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 30210594
StrifeJester.... kinda repeats merowinger and my own VBScript solutions (and suggestions as running from batch)....

Steve
0
 
LVL 43

Expert Comment

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

Author Comment

by:jmhabis2
ID: 30223855
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
 
LVL 31

Expert Comment

by:merowinger
ID: 30223926
just call the following in batch to start a vbscript

cscript.exe yourscript.vbs
0
 

Author Comment

by:jmhabis2
ID: 30224471
thanks a lot
0
 
LVL 43

Expert Comment

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

Author Comment

by:jmhabis2
ID: 30224921
No worry. you are right.
my mistake
0
 
LVL 60

Expert Comment

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

Expert Comment

by:Steve Knight
ID: 30226421
Me too... just been with 4 year old to the chippy to get tea so not all bad!!

Steve
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

591 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