本文介绍了如何处理带有FOR循环的批处理文件中的"net use"命令错误输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在批处理文件中使用net use命令来连接远程位置.
我想将其输出存储到变量中并进行处理.
命令成功完成后,我的代码可以正常工作.

但是,如果出现一些错误(例如密码错误),那么我将无法将错误输出存储在变量中.它直接打印到运行脚本的控制台窗口.请提供以下帮助:

  1. 为什么会这样?
    为什么在命令输出中出现错误时,我无法将输出存储在变量中?
  2. 是否有适当的方法来记录"net use"命令的ERROR输出?

以下是我的代码:

:connNEWlocation
echo Connecting required remote location...
for /f "tokens=1,2,3,4,5,6,7,8 delims=*" %%a IN ('net use  \\!hostaddress!\!user! /user:!user! !user_pass!') DO (
    if "%%a"=="The command completed successfully." (
        echo Connected successfully^!
    ) else (
        echo ERROR:"%%a"
        echo do something based on error obtained here
    )
)
解决方案

将错误输出写入以处理 STDERR (标准错误).但是 FOR 仅捕获为处理 STDOUT 而编写的标准输出,该输出是在以cmd.exe /C为背景的单独命令过程中在两个'之间运行命令行的./p>

该解决方案使用的是2>&1,正如Microsoft在有关使用命令重定向运算符.

在这种情况下,两个运算符>&必须转义为插入符号^,以便在由Window命令处理器解析整个 FOR 命令行时首先被解释为文字字符在完全执行 FOR 命令行之前.

for /F "delims=" %%a in ('net use \\!hostaddress!\!user! /user:!user! !user_pass! 2^>^&1') do (

"delims="禁止使用空格和水平制表符作为分隔符将捕获的行拆分为标记,因为它定义了一个空的分隔符列表.

I'm using net use command in batch file to connect a remote location.
I want to store the output of it into a variable and process it.
When the command completes successfully, my code works fine.

However, if there is some error, like wrong password, then I'm not able to store the error output in a variable. It directly prints to the console window in which script is running. Please help with the following:

  1. Why does this happen?
    Why I'm unable to store output in variable when error occurs in the command output?
  2. Is there a suitable way to log ERROR output of the 'net use' command?

Following is my code:

:connNEWlocation
echo Connecting required remote location...
for /f "tokens=1,2,3,4,5,6,7,8 delims=*" %%a IN ('net use  \\!hostaddress!\!user! /user:!user! !user_pass!') DO (
    if "%%a"=="The command completed successfully." (
        echo Connected successfully^!
    ) else (
        echo ERROR:"%%a"
        echo do something based on error obtained here
    )
)
解决方案

The error output is written to handle STDERR (standard error). But FOR captures only standard output written to handle STDOUT on running the command line between the two ' in a separate command process started with cmd.exe /C in the background.

The solution is using 2>&1 as Microsoft explains in article about Using Command Redirection Operators.

The two operators > and & must be escaped in this case with caret character ^ to be interpreted first as literal characters on parsing the entire FOR command line by Window command processor before the FOR command line is executed at all.

for /F "delims=" %%a in ('net use \\!hostaddress!\!user! /user:!user! !user_pass! 2^>^&1') do (

"delims=" disables splitting up the captured line(s) into tokens using space and horizontal tab character as delimiters because it defines an empty list of delimiters.

这篇关于如何处理带有FOR循环的批处理文件中的"net use"命令错误输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:21
查看更多