0
votes

When I execute the following command from a Windows Batch file...

"e:\cdsdk\direct.exe" -f"e:\cdsdk\MyCred.txt" < "\\MyServer\ConfigFiles\MyParams.CDConfig" > "\\MyServer\MyLog.log"

It successfully executes the direct.exe program with the credentials specified in the MyCred.txt file using the parameters specified in the MyParams.CDConfig. It logs the output to the file MyLog.log.

However, when I run the VB Script below, I get the message

The Following error occurred:

While processing the command line, 'M' was encountered, but a '/' or a '-' was expected.

What do I have to do to get the same command working in VBScript?

SET oFS = CreateObject("Scripting.FileSystemObject")
SET sout = oFS.GetStandardStream(1)
sout.WriteLine("Message to Console: Start")

Dim CDCmd 
Dim Quote 
Quote = CHR(34)
CredFile = "e:\cdsdk\MyCred.txt"
ConfigFile = "\\MyServer\ConfigFiles\MyParams.CDConfig"
CdLogFile = "\\MyServer\MyLog.log"

CDCmd = Quote & "e:\cdsdk\direct.exe" & Quote & " -f" & Quote & CredFile & Quote & " < " & Quote & ConfigFile & Quote & " > " & Quote & CdLogFile & Quote
sout.WriteLine("Message to Console: CDCmd=" & CDCmd)

Set objShell = WScript.CreateObject("WScript.Shell")
Dim ReturnValue
ReturnValue = -1
ReturnValue = objShell.Run (CDCmd, 3, true)
sout.WriteLine("Return Value=" & ReturnValue)

sout.WriteLine("Message to Console: End")

WScript.Quit ReturnValue

When I compare the value of CDCmd written to the Console, it looks the same as the batch command that works when executed in a batch file.

1

1 Answers

2
votes

When you want to use shell features like >, you'll have to execute your command via %comspec% /c (or /k for debugging). If that does not help, the building of the command has to be made sane.

P.S.

WScript.Shell.Run/.Exec starts a process (not a shell); to get shell functionality (like redirection or inbuilds), you must start a shell - e.g. cmd.exe - and let it run the program(s) you need. %comspec% is the system's environment variable that points to your shell:

echo %comspec%
C:\WINDOWS\system32\cmd.exe