- Save as vbs - deletefiles.vbs
- to use pass in number folder and number of days
' Pass the arguments
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
path = Wscript.Arguments.Item(0)
' folder to start search in... You can just use static
'path = "C:\Temp\test"
' delete files older than 2 days...
mDays = Wscript.Arguments.Item(1)
killdate = date() - mDays
arFiles = Array()
set fso = createobject("scripting.filesystemobject")
' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
SelectFiles path, killdate, arFiles, true
nDeleted = 0
for n = 0 to ubound(arFiles)
' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
on error resume next 'in case of 'in use' files...
if err.number <> 0 then
wscript.echo "Unable to delete: " & arFiles(n).path
nDeleted = nDeleted + 1
'start 20100630 schneiderbi
'end 20100630 schneiderbi
on error goto 0
'msgbox nDeleted & " of " & ubound(arFiles)+1 _
' & " eligible files were deleted"
on error resume next
'select files to delete and add to array...
set folder = fso.getfolder(sPath)
set files = folder.files
for each file in files
' uses error trapping around access to the
' Date property just to be safe
dtlastmodified = null
on error resume Next
dtlastmodified = file.datelastmodified
on error goto 0
if not isnull(dtlastmodified) Then
if dtlastmodified < vKillDate then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = file
if bIncludeSubFolders then
for each fldr in folder.subfolders