batch-file - Windows 批处理 : Executing command with FOR/F -- "command not found"

标签 batch-file cmd command-execution

我在 Windows 7 Ultimate 上的 FOR 命令中执行其他命令时遇到问题。
for-command 是批处理文件的一部分,应该解析另一个命令的输出。

通常是这样的:

for /f %%a IN ('tasklist') DO echo %%a

应该执行命令“Tasklist”并通过echo输出其结果。
但我总是得到“找不到命令”。

我试图在 forloop 之外执行命令并且它有效。
我还尝试在 for 中执行许多外部命令,但每个命令都说“找不到命令”。

我也尝试了这篇文章的例子:

Batch: Execute command with quotes in for loop with piping to find

我确定
  • 命令存在
  • 我已阅读和执行它的权利
  • 我的用户在管理员组
  • 我使用“以管理员身份运行”运行命令

  • 但是在这台电脑上没有任何工作。
    同样的命令可以在另一台运行 Windows 7 Ultimate 的 PC 上运行。

    那么,有没有人认为在 PC 上找不到所有命令的想法可能是错误的?

    这是我使用命令“ls”尝试时控制台输出的示例。
    ls.exe 是来自 gun4win 项目的可执行文件,它位于运行我的批处理文件的同一文件夹中。
    窗口是德语的,所以错误输出也是德语的。

    C:\test>test_for.bat
    C:\test>rem --- test a command stand-alone ---   
    
    C:\test>ls 
    ls.exe        test_for.bat     
    
    C:\test>rem --- test same command in a FOR-Loop ---   
    
    C:\test>for /F "delims=" %a in ('ls') do echo FOR-OUTPUT: %a 
    Der Befehl "ls" ist entweder falsch geschrieben oder konnte nicht gefunden
    werden.
    

    最终编辑:
    问题是系统环境变量 ComSpec 的值错误.

    我在 Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen" to "C:\Windows\system32\cmd.exe 中更改了 ComSpec问题就解决了。

    感谢@foxidrive 和@jeb

    最佳答案

    如果您使用 FOR /F并得到错误 'something' is not recognized as an internal or external command, operable program or batch file对于每个程序,甚至是内部命令,最常见的原因是 ComSpec 变量错误。
    您可以使用 set ComSpec 检查变量,应该是 C:\Windows\System32\cmd.exe .
    在命令行上更改变量无济于事,原因在 DosTips: ComSpec strange behaviour 中描述。
    如果变量包含不同的值,您应该在下面更正
    对于德国系统:Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen对于英语系统:Win Key+Pause Key->Advanced System Settings->Environment Variables->System Variables 奇怪的 FOR /F 存在第二个可能的原因行为
    如果可以在注册表中启用 AutoRun 功能 ...\Command Processor\AutoRun , 详情请见 cmd /? .
    AutoRun 功能可以在每次启动新的 cmd.exe 实例时启动一个批处理文件。
    这对 ex 很有用。显示有关打开新 cmd 窗口或始终更改到所选目录的一些数据。
    但是这个batch也会在FOR /F里面执行并且通常会导致意想不到的结果。
    管道也启动新的 cmd 实例,但禁止自动运行脚本

    关于batch-file - Windows 批处理 : Executing command with FOR/F -- "command not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35865137/

    相关文章:

    batch-file - 批处理文件 - 将 USB 从读/写切换为只读,反之亦然

    windows - Perl 在 Windows 上替换 UTF-8 字符串的问题

    cmd - “makecert”在命令提示符中未被识别为内部或外部命令

    syntax - powershell中的条件执行(&&和||)

    java - CommandExecuteIn 后台抛出 "Not an (encodable) value"错误

    batch-file - 批处理文件递归复制目录

    Windows 批处理文件启动带有按钮的 gui

    cmd 中的 Java PID 没有改变

    powershell - 显示颜色代码而不是颜色