I found a VB script to delete files in a dir older than a certain date:
Option Explicit
‘ Path to delete
const FolderName ="c
Temp"
‘ Age of file (last modified) in days before it is deleted
const Days=7
‘Set debug to 1 for screen pops with helpful messages
const debugmode=0
const SUCCESS=0
const ERROR=1
const WARNING=2
const INFORMATION=4
dim result
logit "Begining process to delete temp files in " & FolderName & " older than " & Days & " Days",INFORMATION
result = delFiles(FolderName,Days)
Function delFiles(strDir,strDays)
‘ Take in the path to delete and the number of days old a file has to be
‘ Then delete the files in that path if they are older than that date
dim fso,file,folder,individualFile,foldercontains
dim strComments
dim intDeleted, intNotDeleted
intDeleted=0
intNotDeleted=0
Set fso = CreateObject("Scripting.FileSystemObject")
on error resume next
Set folder = fso.GetFolder(strDir)
if err = 0 then
strComments = strComments & strDir & " exists." & VBCRLF
else
strComments = strComments & ". **ERROR** Accessing folder – cannot find " & strDir & VBCRLF & err.description & " (" & err.source & ")" & VBCRLF
intNotDeleted=intNotDeleted+1
end if
err.Clear
Set foldercontains = folder.Files
dim intDifferenceinDays
strComments = strComments & "Deleting Files older than " & strDays & " days" & VBCRLF
For Each individualFile in folderContains
‘ Loop through each file in the folder and check its date
if debugmode=1 then
wscript.echo ("Looking at " & individualfile & VBCRLF & "Which has date last modified of: " & individualFile.datelastmodified _
& VBCRLF & "To see if its " & strDays & " days older than " & Now)
End if
strComments = strComments & VBCRLF & "Analysed " & individualfile & ": "
intDifferenceinDays = DateDiff("d", individualFile.datelastmodified, Now)
If intDifferenceinDays > strDays Then
if debugmode=1 then
wscript.echo ("We’ve decided to delete" & file &"… Datediff is" & intDifferenceinDays )
End if
strComments = strComments & " Deleting…."
on error resume next
fso.DeleteFile individualFile,TRUE
if err = 0 then
intDeleted=intDeleted+1
strComments = strComments & ". SUCCESS"
else
intNotDeleted=intNotDeleted+1
strComments = strComments & ". **ERROR** " & err.description & " (" & err.source & ")"
end if
err.Clear
Else
if debugmode=1 then
wscript.echo ("We’ve decided to spare" & file &"… Di
" & intDifferenceinDays & " Required: " & strDays)
End if
strComments = strComments & "Not Deleted. Only " & intDifferenceinDays & " days old"
End If
Next
strComments = strComments & VBCRLF & "No of Files Deleted: " & intDeleted & VBCRLF & "ERROR in deleting: " & intNotDeleted
if intNotDeleted > 0 then
logit strComments, ERROR
else
logit strComments,INFORMATION
end if
delFiles=1
End Function
Function logit(text,level)
‘ Writes a simple message to the windows event log
dim Wshshell
set WshShell = CreateObject("WScript.Shell")
WshShell.LogEvent level, text
logit=1
end Function
However, when I try and run this a "test.vbs", I get an invalid character on the "'" comment character, and I cannot determine why.
Option Explicit
‘ Path to delete
const FolderName ="c
Temp" ‘ Age of file (last modified) in days before it is deleted
const Days=7
‘Set debug to 1 for screen pops with helpful messages
const debugmode=0
const SUCCESS=0
const ERROR=1
const WARNING=2
const INFORMATION=4
dim result
logit "Begining process to delete temp files in " & FolderName & " older than " & Days & " Days",INFORMATION
result = delFiles(FolderName,Days)
Function delFiles(strDir,strDays)
‘ Take in the path to delete and the number of days old a file has to be
‘ Then delete the files in that path if they are older than that date
dim fso,file,folder,individualFile,foldercontains
dim strComments
dim intDeleted, intNotDeleted
intDeleted=0
intNotDeleted=0
Set fso = CreateObject("Scripting.FileSystemObject")
on error resume next
Set folder = fso.GetFolder(strDir)
if err = 0 then
strComments = strComments & strDir & " exists." & VBCRLF
else
strComments = strComments & ". **ERROR** Accessing folder – cannot find " & strDir & VBCRLF & err.description & " (" & err.source & ")" & VBCRLF
intNotDeleted=intNotDeleted+1
end if
err.Clear
Set foldercontains = folder.Files
dim intDifferenceinDays
strComments = strComments & "Deleting Files older than " & strDays & " days" & VBCRLF
For Each individualFile in folderContains
‘ Loop through each file in the folder and check its date
if debugmode=1 then
wscript.echo ("Looking at " & individualfile & VBCRLF & "Which has date last modified of: " & individualFile.datelastmodified _
& VBCRLF & "To see if its " & strDays & " days older than " & Now)
End if
strComments = strComments & VBCRLF & "Analysed " & individualfile & ": "
intDifferenceinDays = DateDiff("d", individualFile.datelastmodified, Now)
If intDifferenceinDays > strDays Then
if debugmode=1 then
wscript.echo ("We’ve decided to delete" & file &"… Datediff is" & intDifferenceinDays )
End if
strComments = strComments & " Deleting…."
on error resume next
fso.DeleteFile individualFile,TRUE
if err = 0 then
intDeleted=intDeleted+1
strComments = strComments & ". SUCCESS"
else
intNotDeleted=intNotDeleted+1
strComments = strComments & ". **ERROR** " & err.description & " (" & err.source & ")"
end if
err.Clear
Else
if debugmode=1 then
wscript.echo ("We’ve decided to spare" & file &"… Di
" & intDifferenceinDays & " Required: " & strDays) End if
strComments = strComments & "Not Deleted. Only " & intDifferenceinDays & " days old"
End If
Next
strComments = strComments & VBCRLF & "No of Files Deleted: " & intDeleted & VBCRLF & "ERROR in deleting: " & intNotDeleted
if intNotDeleted > 0 then
logit strComments, ERROR
else
logit strComments,INFORMATION
end if
delFiles=1
End Function
Function logit(text,level)
‘ Writes a simple message to the windows event log
dim Wshshell
set WshShell = CreateObject("WScript.Shell")
WshShell.LogEvent level, text
logit=1
end Function
However, when I try and run this a "test.vbs", I get an invalid character on the "'" comment character, and I cannot determine why.