错误:"|"此时是意外的批处理脚本
问题描述:
运行此脚本时,我收到
|这是出乎意料的.
| was unexpected at this time.
这是代码:
@ECHO Off
REM Mapeo de unidad U, path de usuario
net use u: \\server\usuarios\%username%
pause
REM ***** Description group ****
for /f %%i in ('net user %username% /domain | find /i /c Group') do set RESULT=%%i
echo %RESULT%
pause
在for
句子中,i
使用"
和'
,但是仍然出现错误.
In for
sentence, i
use "
and '
, but I still got error.
答
批处理解析器必须先解析for
内部的条件,然后才能将其作为可执行命令传递给IN()子句,并且因为管道是DOS中的特殊字符,您需要在管道之前使用转义字符(^)来在初始批处理分析期间保留它,如下所示:
The condition inside the for
must be parsed by the batch parser before it can pass it to the IN() clause as an executable command and since the pipe is a special character in DOS, you need to use escape character(^) before pipe to preserve it during the initial batch parsing, as shown below:
for /f %%i in ('net user %username% /domain ^| find /i /c Group') do set RESULT=%%i