WinBatch® Technical Support Forum

Archived Boards => COM Automation and dotNet => Topic started by: mathia on November 01, 2016, 10:23:17 AM

Title: Wscript.shell.exec vs command line
Post by: mathia on November 01, 2016, 10:23:17 AM
I have tried everything I can think of to get this to work, but I'm still getting an error. I must note that it works when run from the command line.

objShell=objectcreate("Wscript.Shell")
boxtext('Verifying Files')
command='"c:\windows\system32\xxcopy.exe" %drive%\ %root%\ /s /v2 /pb /xoptima3.id | "C:\Program Files (x86)\GnuWin32\bin\tee.exe" temp1.txt'
;command='c:\xxcopy64\xxcopy.exe %drive%\ %root%\ /s /v2 /pb /xoptima3.id'
objExec = objShell.Exec ('%command%')

The error I get is:
COM/CLR Exception
Wshell.exec
System cannot find the file specified

The version with all the parameters filled in works only on command line as well:

"c:\windows\system32\xxcopy.exe" D:\ t:\stmts\ /s /v2 /pb /xoptima3.id | "C:\Program Files (x86)\GnuWin32\bin\tee.exe" temp1.txt
Title: Re: Wscript.shell.exec vs command line
Post by: td on November 01, 2016, 01:36:02 PM
The 'Wscript.shell' COM Automation object and the  'cmd.exe' Windows command shell are not interchangeable.   Specifically, a pipe (|) is processed by 'cmd.exe' but you are using 'wscript.shell' which knows nothing about piping standard output to standard input using '|'. 

So you need to use cmd.exe.  Something like the following:

Code (winbatch) Select
objShell = ObjectCreate('wscript.shell')
objShell.Run('cmd /c dir c:\ | sort > c:\temp\tmp.txt',0,0)


Also, using quotes and variable substitution as illustrated in your example is just wasting CPU cycles. 
Title: Re: Wscript.shell.exec vs command line
Post by: mathia on November 01, 2016, 02:15:49 PM
I can't use the Run method because I'd like to get the stdout without having to parse the file it's being redirected to.  Also, I'm piping it to the Tee command so that I can see the output on the screen as well as get the stdout.  What I'm doing might just not be possible, though.

I'm only using substitution because there are multiple possible paths and outcomes from the Select that you don't see in this piece of code.  Probably poor form on my part.

Thanks for taking time to answer.
Title: Re: Wscript.shell.exec vs command line
Post by: td on November 01, 2016, 02:40:53 PM
Quote from: mathia on November 01, 2016, 02:15:49 PM
I can't use the Run method because I'd like to get the stdout without having to parse the file it's being redirected to.  Also, I'm piping it to the Tee command so that I can see the output on the screen as well as get the stdout.  What I'm doing might just not be possible, though.

Using tee.exe does not prevent you from using the 'run' method to see the ouput in stdout.  Just change the '/c' command switch to '/k' to leave the command shell window up.

Quote
I'm only using substitution because there are multiple possible paths and outcomes from the Select that you don't see in this piece of code.  Probably poor form on my part.

Sill not a reason to use substitution, since using single quotes around your substitution works. 

And just to be clear the original response was not about using the 'run' vs. 'exec' method.  It was about using the command interpreter, cmd, so that you can use the pipe.
Title: Re: Wscript.shell.exec vs command line
Post by: td on November 01, 2016, 02:52:53 PM
Or if you want to capture stdout to a WIL variable follow the link to an example.

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/DOS+Get~results~from~STDOUT~-~UDF.txt (http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/DOS+Get~results~from~STDOUT~-~UDF.txt)