7

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

Radon8472
  • 4,285
  • 1
  • 33
  • 41
  • 3
    Does your script use a variable with the name `path` ? – foxidrive Mar 08 '16 at 10:52
  • 2
    ...or `pathext`? you could try to execute the script in the directory `%SystemRoot%\System32\`, where the `tasklist` command is located... – aschipfl Mar 08 '16 at 12:04
  • That doesn`t mater where the command is located. I even tried a command wich is laying in the same folder like my batchfile. Its says "comand not found" for everything. But I can execute all commands outsite of the for. Only within the for no comand is found – Radon8472 Mar 08 '16 at 14:55
  • Unlike `'something' is not recognized as an internal or external command, operable program or batch file`, "command not found" does not seem to be any standard Windows `cmd` error message. Please [edit] your question and add any _real_ particular sample satisfying [mcve]. Copy & paste from `cmd` window having `echo ON` active in your batch script. – JosefZ Mar 08 '16 at 19:32
  • 1
    @JosefZ `"command not found" does not seem to be any standard Windows cmd error message.` <--- Good point Josef. It's not a cmd error message. – foxidrive Mar 08 '16 at 22:25
  • 1
    If the error message was misquoted, the `comspec` variable might be fubar also, if commands run inside the script but only fail within a for command - as comspec is used to call cmd.exe for each of those commands inside the for. – foxidrive Mar 08 '16 at 22:27
  • command not found was yust a simple translation of the german error message, but now a added example output of my testscript to the question. Full translated errormessage would be: `Command "tasklist" is written wrong or cannot be found` And ComSpec=C:\Temp – Radon8472 Mar 09 '16 at 08:18
  • @Radon8472 `comspec` should be `C:\Windows\system32\cmd.exe`, as this is the program that will be started in the for loop. If it's really `C:\temp` on your system then FOR /F can't work – jeb Mar 09 '16 at 08:20
  • More at [DosTips: ComSpec strange behaviour](http://www.dostips.com/forum/viewtopic.php?f=3&t=7006) – jeb Mar 09 '16 at 08:42
  • Setting ComSpec to "C:\Windows\System32\cmd.exe" does not change anything :( – Radon8472 Mar 09 '16 at 09:21
  • @Radon8472 If you open a `new` cmd window and type `set comspec` what appears on your console? – foxidrive Mar 09 '16 at 09:27
  • 1
    As said, changing ComSpec will be cached. Check/modify your ComSpec setting under `Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen` – jeb Mar 09 '16 at 09:30
  • Changing the System ENV "ComSpec" to "C:\Windows\System32\cmd.exe" works now :) If you enter this hint as a Answer, you get the Accepted Answer Flag. Thank you :) – Radon8472 Mar 09 '16 at 09:39

1 Answers1

5

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

jeb
  • 78,592
  • 17
  • 171
  • 225