VBSctipt (Windows Scripting Host) help Wscript.Quit not working

I have been trying to debug a VBScript that runs on a schedule every 10 minutes to move folders and their contents from one network location to another.

I don’t have access to the console, only the folder where the script lives (which is also where it writes a log file). I can see that it’s running, because it writes to the log.
I inherited this script from someone else who has left - VBS is not my native coding language, but I’m comfortably familiar with VBA, which is nearly identical.

Here’s the first bit of the script:



On Error Resume Next
ForAppending = 8
Scriptversion = 1.7

Set objShell = wscript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
TempFolderPath = "\\[ip address redacted]\filesIn" 'no trailing backslash - this is the location of the incoming folders

Set ObjOutFile = objFSO.OpenTextFile("\\[server path redacted]\Moved Files List.txt", ForAppending, True)  

ObjOutFile.WriteLine(vbCRLF & Now & "- Script version " & scriptversion)   

Set objFolder = objFSO.GetFolder(TempFolderPath)
If objFolder.Subfolders.count = 1 Then
	ObjOutFile.WriteLine("Nothing to do")
	Wscript.Quit 
End If

ObjOutFile.WriteLine("There are " & objFolder.Subfolders.count & " folder(s) to process.")
For Each objSubfolder In objFolder.Subfolders

[code here that moves folders and their contents, etc.


Everything is working, except the Wscript.Quit

If there’s only 1 folder in the source location, I want it to report ‘nothing to do’, then quit (the single folder is one I know must not be moved - ignored by the later code), but when I examine the log file, I’m seeing this:


10/01/2014 16:30:42  Script version 1.7
Nothing to do
**There are 1 folder(s) to process. 
---------
Next folder is: Archive
Archive is out of scope for this job**

None of the graped log entries should ever appear if the first line is ‘nothing to do’ - because the code that writes ‘nothing to do’ is immediately followed by Wscript.Quit

So what could be amiss here?

I don’t see a problem with the code. All I can tell you to try is disabling “On Error Resume Next” for the duration of the If statement, and see if you get an error. Because if, somehow, Wscript.Quit is giving out an error, it will be skipped.

I believe the statement to disable error handling is “On Error GoTo 0”.

BTW, as a workaround, you could always put the entire rest of the script in an Else block, and end the script with Wscript.Quit.

The only reason WScript.Quit is giving out an error I can think of is if you’re running a VBScript custom action in a MSI file as Windows Installer does not directly support WScript objects. But you’re not doing that so I got nothing.

Tried your code on my computer and it worked perfectly fine.

Excellent suggestion - I’ll give that a go. Thanks.