【问题标题】:for /f is not working in .bat file but it works with command prompt in windows 2003for /f 不适用于 .bat 文件,但它适用于 Windows 2003 中的命令提示符
【发布时间】:2015-08-26 10:06:06
【问题描述】:

for /f "tokens=1-7 delims=,:" %a in ('query user ^| find /i "disc"') do logoff %b

以上代码用于注销远程桌面用户,在 Windows 2003 中状态为“已断开连接”。当我在命令提示符下运行时,它将完美运行。但是当我在 windows 2003 中创建 .bat 文件或 .cmd 文件时它不会运行。所以可能知道我哪里出错了?

【问题讨论】:

    标签: command-line command-prompt windows-server-2003


    【解决方案1】:

    在批处理文件中,for 可替换参数中使用的百分号需要转义

    for /f "tokens=1-7 delims=,: " %%a in ('query user ^| find /i "disc"') do logoff %%b
    

    【讨论】:

    • 不,它不工作它的 2003 Windows 服务器。我试过了。
    • @user2503585,它怎么不起作用?观察到的行为是什么?
    • 它只是闪烁 cmd 提示并消失。
    • @user2503585,将命令单独放在批处理文件中,打开cmd 窗口并从中执行批处理文件。你看到了什么?
    • b 在这个时候出乎意料
    【解决方案2】:

    用户 585,

    是的,为了在 bat/cmd 会话中实现 for 循环,您需要将变量与

    %%a
    

    喜欢这个

    for /f %%a in (.\hosts) do quser /server:\\%%a
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 2021-05-02
      • 2013-01-24
      • 1970-01-01
      • 2013-02-23
      相关资源
      最近更新 更多