net use - How to process 'net use' command error output in batch file with a FOR loop? -
i'm using net use command in batch file connect remote location.
want store output of variable , process it.
when command completes successfully, code works fine.
however, if there error, wrong password, i'm not able store error output in variable. directly prints console window in script running. please following:
- why happen?
why i'm unable store output in variable when error occurs in command output? - is there suitable way log error output of 'net use' command?
following code:
:connnewlocation echo connecting required remote location... /f "tokens=1,2,3,4,5,6,7,8 delims=*" %%a in ('net use \\!hostaddress!\!user! /user:!user! !user_pass!') ( if "%%a"=="the command completed successfully." ( echo connected successfully^! ) else ( echo error:"%%a" echo based on error obtained here ) )
the error output written handle stderr (standard error). for captures standard output written handle stdout on running command in separate command process in background.
the solution using 2>&1 microsoft explains in article using command redirection operators.
the 2 operators > , & must escaped in case caret character ^ interpreted first literal characters on parsing entire for command line window command interpreter.
for /f "delims=" %%a in ('net use \\!hostaddress!\!user! /user:!user! !user_pass! 2^>^&1') ( "delims=" disables splitting captured line(s) tokens using space , horizontal tab character delimiters because defines empty list of delimiters.
Comments
Post a Comment