Link to home
Start Free TrialLog in
Avatar of cwrea
cwreaFlag for Canada

asked on

How to redirect stderr to file using CMD.EXE and Shell() in VB?

I've got an app where I am calling the VB Shell function to execute a command.  In order to capture the command's output, I've actually got it running with "CMD /C myprog.exe >myprog.out" ... now that works fine usually and I get the result of the execution in the file myprog.out

However when something goes wrong, the errors the program writes don't go into the file.  In C parlance, it looks like it is writing to stderr in exceptional circumstances.

How can I redirect that output as well to a file?  Under UNIX I could specifically tell it to output a particular file number such as stderr; any way to do that with CMD.EXE under NT?

Thanks

Avatar of cwrea
cwrea
Flag of Canada image

ASKER

BTW... when I simulate the particular command at a command prompt manually by typing "CMD /C myprog.exe >myprog.out", I can see SOME lines go to the screen (what I believe are written to stderr) and SOME lines go to the file (what I believe are written to stdout)

ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of posbis
posbis

Works much easier.... :-)

As in UNIX you just must redirect "stderr" ot "stdout"

"CMD.EXE /C myprog.exe > myprog.out 2>&1"

Yes :-) this works under Windows NT as well *smile* but most people don't know it.

The CMD.EXE has some powerful features starting from Windows 2000/XP.
OK, they don't have the power of a BASH shell but you can do a lot believe me :-)