John E
John E

Reputation: 25

Unable to run VBS as Batch or Scheduled Task

I have a VBS that prints a set of files to PDF. Works fine when running the VBS manually, but when trying to execute either via Batch or Scheduled Task it does not work. I have tried the following.

Batch File:

C:\APPS\TEMP\example.vbs

Result:

enter image description here

Batch File:

%windir%\syswow64\cscript //nologo C:\APPS\TEMP\example.vbs

Same result as image shown above.

VBS Code:

Set fso = CreateObject("Scripting.FileSystemObject")
currentdir = fso.GetAbsolutePathName(".")

Set xmldom = CreateObject("MSXML.DOMDocument")
xmldom.Load(currentdir & "\info.xml")

progid = xmldom.SelectSingleNode("/xml/progid").text

Set obj = CreateObject(progid)

printername = obj.GetPrinterName

runonce = obj.GetSettingsFileName(True)

Set fldr = fso.GetFolder(currentdir & "\in")
cnt = 0
For Each f In fldr.files
  cnt = cnt + 1
  output = currentdir & "\out\" & Replace(f.name, ".xls", "") & ".pdf"

  obj.Init
  obj.SetValue "Output", output
  obj.SetValue "ShowSettings", "never"
  obj.SetValue "ShowPDF", "no"
  obj.SetValue "ShowProgress", "no"
  obj.SetValue "ShowProgressFinished", "no"
  obj.SetValue "SuppressErrors", "yes"
  obj.SetValue "ConfirmOverwrite", "no"

  obj.WriteSettings True

  printfile = currentdir & "\in\" & f.Name
  cmd = """" & currentdir & "\printto.exe"" """ & printfile & """ """ & printername & """"

  Set WshShell = WScript.CreateObject("WScript.Shell")
  ret = WshShell.Run(cmd, 1, True)

  While fso.fileexists(runonce)
    WScript.Sleep 100
  Wend
Next

Set obj = Nothing

WScript.Echo cnt & " documents were printed."

In order to get it to run manually I had to set 32-bit cscript.exe as the default program for VBS. So in my mind, the batch code that calls up that first them points to the VBS should work, but I am not sure why it isn't. This in a Windows Server 2008 R2 system. Also tried it on a Windows 7 x64 box to eliminate any odd GPO or security issues.

Upvotes: 2

Views: 1221

Answers (2)

Kul-Tigin
Kul-Tigin

Reputation: 16950

Works fine when running the VBS manually

Suppose that your info.xml is located in the same directory as the script file C:\APPS\TEMP\info.xml.

fso.GetAbsolutePathName(".") gives you the current, by default startup directory. It's exactly the same as running CD . on a command prompt.

Your script works when you run by double-clicking because it starts from it's parent folder.

But it does not work when you start from C:\APPS as in the image you show or as a task command with no start-in directory configuration. A task command starts from the user's home directory, commonly %windir%\system32 when you dismiss to configure start in directory.

In short, the file info.xml cannot be found so the method SelectSingleNode failed.

I'd recommend you to to locate info.xml by using the script's full path, instead of fso.GetAbsolutePathName(".").

currentdir = fso.BuildPath(fso.GetParentFolderName(WScript.ScriptFullName), "info.xml")

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

Presumably there was an error when loading the XML data. Insert code to check for parser errors after you load the file. Also tell the parser to work synchronously, so you don't try to do something when the previous operation isn't completed yet.

xmldom.Async = False
xmldom.Load(currentdir & "\info.xml")

If xmldom.ParseError.ErrorCode <> 0 Then
  WScript.Echo xmldom.ParseError.Reason
  WScript.Quit xmldom.ParseError.ErrorCode
End If

progid = xmldom.SelectSingleNode("/xml/progid").text

Also make sure that the file you're trying to read is where you expect it to be. The statement

currentdir = fso.GetAbsolutePathName(".")

produces the path to the current working directory, but in a scheduled task that might be different from what you expect, unless you explicitly define a working directory in the task properties.

Upvotes: 1

Related Questions