I've pieced together a script -- parts are borrowed
, parts are original -- that traverses through the sub-directories of a defined directory and deletes temporary files older than 90 days. It's also supposed to log every file that's deleted.
The script works as intended when run from Windows Server 2008 (x86) SP2 using wscript.exe & cscript.exe. When run, the process' CPU utilization hovers around 15-20% and logs every file that's deleted. However, when set to run as a Scheduled Task, the wscript.exe process starts, but nothing ever happens.
The wscript.exe process never jumps over 0% and the script never deletes or logs any files.
The scheduled task is set to run whether a user is logged on or not. It is also set to run as a user with administrative privileges.
After encountering this, I wrote a batch script to invoke the VB script and had the some results: when run manually, it worked; when run as a scheduled task, it did not. (Interestingly, when the batch script is run as a scheduled task, the cscript.exe process immediately started and stopped and the task history shows the jobs ran successfully, even though nothing was deleted or logged.)
Here's the code:
Const strSrcPath = "D:\Desktop Backup\"
Const intMaxDate = 90
Dim objFSO, deleteLog
Set objFSO = CreateObject("Scripting.FileSystemObject")
set deleteLog = objFSO.OpenTextFile("C:\sync\logs\deletedfiles.txt",8,True)
TraverseFolder strSrcPath, intMaxDate
Sub TraverseFolder(strSrcPath, intMaxDate)
Set objCurrentFolder = objFSO.GetFolder(strSrcPath)
On Error Resume Next
if (InStr(objCurrentFolder,"Local Settings\Temporary Internet Files") OR InStr(objCurrentFolder,"Local Settings\Temp\")) then
Dim objFile, objFolder
For Each objFile In objCurrentFolder.Files
If DateDiff("d", CDate(objFile.DateLastModified), Now) > intMaxDate Then
deleteLog.WriteLine objFile & "|" & objFile.Size & " bytes" & "|" & objFile.DateLastModified & "|" & Now
For Each objFolder In objCurrentFolder.subFolders
TraverseFolder objFolder, intMaxDate
Is there a restriction against using VB file system objects or recursion when calling VB scripts from a scheduled task? (That wouldn't really make a lot of sense to me, but it's the best explanation I've been able to come up with!)