The following is a purge file script i wrote. it takes three arguments, folder to be purged, log file, and the age of the files/folders to be deleted:
Dim t_date, p_date, date_diff, DateCheck, Output, stream, argv
Dim log_file, t_age, num_files, num_folders, temp_files
Const ReadOnly = 1
'on error resume next
sub doFolder(myFolder, date_age)
date_age = CInt(date_age)
set objFiles = myFolder.files
for each objfile in objFiles
wscript.echo objFolder2.path & vbcrlf
't_date = objfile.datelastmodified
t_date = objfile.datecreated
p_date = left(t_date, (InStr(1, t_date, " ") - 1))
date_diff = CInt(datediff("d", p_date, Date))
If (date_diff > date_age) then
Output = Output & "deleting " & objfile.path & " (" & (date_diff) & " days old)" & vbcrlf
'wscript.echo "deleting " & objfile.path & " (" & (date_diff) & " days old)" & vbcrlf
' Change the Files / Folders to Writeable
If (objfile.Attributes AND ReadOnly) Then
Output = Output & "*** " & objfile.name & " is readonly, changing to writeable" & vbcrlf
'wscript.echo objfile.name & " is readonly, changing to writeable" & vbcrlf
objFile.Attributes = objFile.Attributes XOR ReadOnly
End If
objfile.delete
num_files = num_files + 1
Else
Output = Output & "skipping " & objfile.path & " (" & (date_diff) & " days old)" & vbcrlf
end If
next
' Now check all the sub directories recursively
set objFolders = myFolder.subfolders
for each objFolder2 in objfolders
call doFolder (objFolder2, date_age)
wscript.echo objFolder2.path & vbcrlf
't_date = objFolder2.datelastmodified
t_date = objFolder2.datecreated
p_date = left(t_date, (InStr(1, t_date, " ") - 1))
date_diff = CInt(datediff("d", p_date, Date))
set objfiles2 = objfolder2.files
temp_files = 0
for each objfile2 in objfiles2
temp_files = temp_files + 1
next
' Change the Files / Folders to Writeable
If (objFolder2.Attributes AND ReadOnly) Then
Output = Output & "*** " & objFolder2.name & " is readonly, changing to writeable" & vbcrlf
'wscript.echo objfile.name & " is readonly, changing to writeable" & vbcrlf
objFolder2.Attributes = objFolder2.Attributes XOR ReadOnly
End If
If (temp_files = 0 AND date_diff > date_age) Then
'Delete Folder, too
Output = Output & "deleting " & objFolder2.path & " (" & date_dff & " days old)" & vbcrlf
num_folders = num_folders + 1
objFolder2.delete
Else
'Output = Output & "skipping " & objfolder.path & " (" & (date_diff) & " days old)" & vbcrlf
End If
next
end sub
'Get Arguments
Set argv = Wscript.Arguments
If argv.count <> 3 Then
'wscript.echo "################################################################"
'wscript.echo "File Purger v.99!" & vbcrlf
'wscript.echo "usage: " & wscript.scriptname & " <purge_path> <log_file> <delete_age>"
'wscript.echo "example: " & wscript.scriptname & "c:\temp c:\purge_log.txt 7"
'wscript.echo "the above example will remove all file older than 7 days in c:\temp, "
'wscript.echo "and log them in c:\purge_log.txt"
'wscript.echo "################################################################"
wscript.quit
End If
purge_path = argv(0)
log_file = argv(1)
t_age = argv(2)
num_files = 0
num_folders = 0
'Wscript.Echo "Starting Purger: " & date & " @ " & time
'Wscript.Echo "Removing objects older than " & t_age & " days"
set objFileSys=createobject("scripting.filesystemobject")
set objFolder = objfilesys.GetFolder (purge_path)
call doFolder(objFolder, t_age)
' Write it all to disk, 8 = append
Set stream = objFileSys.OpenTextFile(log_file, 8, true)
stream.write "#########################################################" & vbcrlf & _
"Starting Purger: " & date & " @ " & time & vbcrlf & _
"Config: delete age: " & t_age & " days | " & _
"log path: " & log_file & vbcrlf & _
"deleted: files: " & num_files & " folders: " & num_folders & vbcrlf & _
"#########################################################" & vbcrlf
stream.write Output & vbcrlf & vbcrlf
stream.close
'Wscript.echo "deleted: files: " & num_files & " folders: " & num_folders
'Wscript.echo "Log Written!"
It works great. It is recursive, and only deletes files and folders with creation dates older than x days.
The problem is that once it reads 255 folders, it crashes out. The error is 'invalid use' or something like that, of
p_date = left(t_date, (InStr(1, t_date, " ")
t_date is the only variable used, but its coming straight from the t_date = objfile.datecreated. This makes me believe there is a variable that is being set too small (2^8) when the set objFileSys=createobject("scripting.filesystemobject") is created.
Does anyone know of a fix for this, or maybe why its happening? |