All commands error when running in my for /f loop "not recognised as an internal or external command"

StackOverflow https://stackoverflow.com/questions/23503595

  •  16-07-2023
  •  | 
  •  

Question

I have a bizarre issue which I hope someone can help me with. I have a batch script that uses a FOR /F loop to read from a file and set variables accordingly. The command is written as follows:

FOR /F "skip=1 tokens=1-3 delims=," %%A in ('type "C:\ReconfigureSettingsFile.csv"') do (commands)

The error is:

'type "C:\ReconfigureSettingsFile.csv"' is not recognized as an internal or external command, operable program or batch file.

It happens even if I run this:

for /f "usebackq delims==" %i in (`set`) do @echo %i

'set' is not recognized as an internal or external command, operable program or batch file.

Anyone any ideas?

Was it helpful?

Solution

There are several problems:

  1. If you use /f, you tell the for-loop to read a file. Don't mix that with commands.
  2. If you use backticks (`), you tell the for-loop to execute a command. You have used straight ticks (')
  3. If you want to use quotation marks for the file name, use usebackq

So either use

FOR /F "skip=1 tokens=1-3 delims=," %%A in (C:\ReconfigureSettingsFile.csv) do (commands)

without type and without quotes or

FOR /F "usebackq skip=1 tokens=1-3 delims=," %%A in ("C:\ReconfigureSettingsFile.csv") do (commands)

without type but with quotes or

FOR "usebackq skip=1 tokens=1-3 delims=," %%A in (`type "C:\ReconfigureSettingsFile.csv"`) do (commands)

with type but without /F.

Don't forget to use %%A, %%B and %%C in the commands.

OTHER TIPS

If the value of the %COMSPEC% environment variable is wrong, FOR /F with a command will report the "not recognized as an internal or external command" error.

In the registry, HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\ComSpec should have a type of REG_EXPAND_SZ and be set to %SystemRoot%\system32\cmd.exe (at least on Windows 7).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top