批处理命令和错误处理

Batch command and error handling

我在批处理文件中使用此代码:

for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1

看起来无论您输入什么 %user%,它仍然会将 %i%+1 设置为 1... 即使未找到 %user%。

如果未找到 %user%,NET USER 报告说未找到用户,但因为 %i%=1 它会在脚本中继续。

我将如何让它发挥作用? 如果在 /Domain 中找不到 %user%,则 %i% 需要 = 0

for /f %%f in ('"net user %user% /domain ^| findstr /i %group%"') do set /a i=%i%+1

您需要转义管道,因为它的优先级高于 for 循环。