Windows Batch: Executing command with FOR /F -- "command not found"

Viewed 2463

I have a problem with executing an other command within a FOR-command on Windows 7 Ultimate. The for-command is part of a batch-file and should parse the outputs of another command.

Usually this:

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

should execute the command "Tasklist" and output its results via echo. But I always get "command not found".

I tried to execute the command outside of the forloop and it works. I also tried do execute lots of outer commands within the for, but every command said "command not found".

I also tried the examples of this post:

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

I`m sure

  • that the commands exists
  • that I have read and execute rights to it
  • that my User is in Administrator Group
  • that I run the commands with "Run as Administrator"

But nothing is working in this pc. The same commands work on another PC wich is also running a windows 7 ultimate.

So has anybody an idea would could be wrong on the pc where all commands are not found ?

Here is an example of my console outputs when I try it with the command "ls". ls.exe is a executable file from the gun4win project, and its located in the same folder where my batch-file is running. The windows is in german, so the error output is also in german.

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.

FINAL EDIT: The problem was as wrong value for the system envoirement variable ComSpec.

I changed ComSpec in Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen" to "C:\Windows\system32\cmd.exe and the problem was solved.

Thanks to @foxidrive and @jeb

1 Answers
Related