I have a really simple for loop I'm trying to do in a windows CMD prompt/batch file (I've tried both with %i in command line and %%i in batch file):

for %%i in (workspace\*) do echo %%i

I wan't to display all the sub folders in a folder (just as a really simple example to get the loop working) but nothing is displayed.

What am I doing wrong here?

  • Works fine here on Windows 7. What is the output of dir workspace? Do you have the correct privileges on the workspace folder?
    – DavidPostill
    Jun 8, 2016 at 11:08
  • I'm on windows 7 too. dir workspace from the same command prompt outputs the list of folders I want to run the command across. This is why I feel like I'm banging my head against a wall here. This should be so simple and "just work" but it is just doing nothing.
    – Tim B
    Jun 8, 2016 at 11:11
  • Weird. Does: for /f "usebackq" %i in (`dir /s/b` ) do @echo %i work?
    – DavidPostill
    Jun 8, 2016 at 11:14
  • @DavidPostill I think it mangled the command in the comment?
    – Tim B
    Jun 8, 2016 at 11:17
  • Should be fixed. You need backticks ` around dir /s/b
    – DavidPostill
    Jun 8, 2016 at 11:18

2 Answers 2


By default for displays only files, not directories, so a directory containing only subdirectories will show nothing. To show directories you need to add the /d option:

for /d %%i in (workspace\*) do echo %%i

This assumes the command is in a batch file (only single % signs if typed), and shows only directories. There is no option to show both: you would need to use a command such as DavidPostill suggests:

for /f "usebackq" %%i in (`dir /b`) do echo %%i
  • 1
    forfiles also finds folders. ;)
    – LPChip
    Jun 8, 2016 at 12:29

If you are running this from the command prompt, and not a batch file, you need to use %i, not %%i.

Alternatively, you can use forfiles which has different options and may very well work a lot easier for what you need to do.

Forfiles also finds folders.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .