1

I have a problem with for loop in batch script. When I try:

for /f "delims=;" %g in ('dir') do echo %g%

i see this

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

Did I miss somethig? Why windows command doesn't work?

3
  • The dir needs to go between backticks. See the output of help for Commented Sep 11, 2015 at 14:52
  • 3
    @a_horse_with_no_name: Only with "usebackq". Otherwise single quotes are fine. Commented Sep 11, 2015 at 15:27
  • 1
    I guess your batch file contains more code than only this line? maybe the error is caused somewhere else...? Commented Sep 11, 2015 at 15:44

3 Answers 3

1

Don't do that. You get all kinds of problems iterating over dir output with for /f. Instead just use

for %g in (*) do @echo %g
Sign up to request clarification or add additional context in comments.

3 Comments

okej, but when a use any other command like git status Why I get same error? Git is added to path and I can use it form cmd without problems, but when i use it in loop I i get this message.
Iterating over dir in a for /f loop is perfectly fine if you use dir /b.
@SomethingDark: Have fun with spaces. You can solve those with appropriate delims, sure. Have fun with Unicode file names in console windows with raster fonts, then. It's sort of like asking »Why would I ever use Directory.GetFiles if I could just as well start cmd /c dir /b and parse its output?«
1

In your question you stated echo %g% which is wrong (the trailing % will be returned literally).

When the command is directly typed into command prompt, use:

for /f "delims=" %g in ('dir /B') do echo %g

When you are using for within a batch file you need double-% for its variable:

for /f "delims=" %%g in ('dir /B') do echo %%g

The option delims=; makes no sense as dir does not give a semicolon-separated list, so I deactivated delims.

The /B switch changes the dir output to bare format (no headers ad footers, only files and dir.s).

1 Comment

The error message would have been "g was unexpected at this time." in that case, though.
0

It looks like the %comspec% variable has been nulled or changed and it can't find cmd.exe

Test this to see the error, and remove line 2 for it to work fine, on a working machine.

@echo off
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause

Something that is very interesting is that the path to cmd.exe is cached.
The two scripts below are the same with only the order of the commands changed

This batch file will fail with both for commands

@echo off
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause
set "comspec=%windir%\System32\cmd.exe"
for /f "delims=;" %%g in ('dir') do echo %%g
pause

and this batch file will work with both for commands.

@echo off
set "comspec=%windir%\System32\cmd.exe"
for /f "delims=;" %%g in ('dir') do echo %%g
pause
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause

1 Comment

Maybe thats the problem. I will try to check comspec in runtime

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.