Author Topic: Wscript.shell.exec vs command line  (Read 456 times)

mathia

  • Newbie
  • *
  • Posts: 40
Wscript.shell.exec vs command line
« 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

td

  • Tech Support
  • *****
  • Posts: 1957
    • WinBatch
Re: Wscript.shell.exec vs command line
« Reply #1 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
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. 
"DON'T PANIC”
The Hitchhiker's Guide to the Galaxy, Douglas Adams


mathia

  • Newbie
  • *
  • Posts: 40
Re: Wscript.shell.exec vs command line
« Reply #2 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.

td

  • Tech Support
  • *****
  • Posts: 1957
    • WinBatch
Re: Wscript.shell.exec vs command line
« Reply #3 on: November 01, 2016, 02:40:53 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.
"DON'T PANIC”
The Hitchhiker's Guide to the Galaxy, Douglas Adams


td

  • Tech Support
  • *****
  • Posts: 1957
    • WinBatch
Re: Wscript.shell.exec vs command line
« Reply #4 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
"DON'T PANIC”
The Hitchhiker's Guide to the Galaxy, Douglas Adams