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
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
If you use FOR /F
and get the error 'something' is not recognized as an internal or external command, operable program or batch file
for every program, even internal commands, the most common cause is a wrong ComSpec variable.
You can check the variable with set ComSpec
, it should be C:\Windows\System32\cmd.exe
.
It will not help to change the variable on the command line, the cause is described at DosTips: ComSpec strange behaviour
If the variable contains a different value, you should correct this under
For a German system:Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen
For an English system:Win Key+Pause Key->Advanced System Settings->Environment Variables->System Variables
There exists a second possible cause for strange FOR /F
behaviour
If the AutoRun feature can be enabled in the registry ...\Command Processor\AutoRun
, for more details see cmd /?
.
The AutoRun feature can start a batch file each time a new cmd.exe instance is started.
This can be useful for ex. showing some data on opening a new cmd window or always change to a choosen directory.
But this batch will be also executed inside the FOR /F
and normally causes unexpected results.
Pipes also start new cmd instances, but suppress the AutoRun script
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With