Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

VB Script hangs when called via Scheduled Task

Avatar of cadcollin
cadcollinFlag for United States of America asked on
Visual Basic ClassicVB Script
5 Comments1 Solution2070 ViewsLast Modified:
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:
Option Explicit
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)
	Dim objCurrentFolder
	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
				objFSO.DeleteFile objFile
			End If
	end if
	For Each objFolder In objCurrentFolder.subFolders
		TraverseFolder objFolder, intMaxDate
End Sub

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!)
Avatar of cadcollin
cadcollinFlag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers