25
for /r %%i in (*) do (echo %%i)

Results in

%%i was unexpected at this time

Why?

dbenham
  • 127,446
  • 28
  • 251
  • 390
citronic
  • 9,868
  • 14
  • 51
  • 74

3 Answers3

40

You must be trying to run the command from the command line and not from within a batch file. Use a single % instead of two when running from the command line.

for /r %i in (*) do (echo %i)

Type HELP FOR from the command line and read the 3rd paragraph.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 2
    If you see "I was unexpected at this time", read http://stackoverflow.com/questions/14509652 before cutting-and-pasting this answer into your file. – Leo Aug 19 '16 at 20:18
0

If Anyone is wondering how to extract the data to a file

using CMD

All files

for /r %i  in (*) do @echo %~ni >> AllFiles.txt

For a specific extension

for /r %i  in (*.mp3) do @echo %~ni >> songs.txt

File name with extension

for /r %a in (*) do @echo %~nxa >> FileNameWithExtension.txt

Note: If you're making a batch script use "%%" instead of every "%" mentioned above

Sal7_one
  • 427
  • 2
  • 7
-1

Syntax:

FOR /R [[drive:]path] %%parameter IN (set) DO command

Need the path before %%i... which is why it's Unexpected

If you want to do * for current directory, just use ".\" for the path

for /r ".\" %%i in (*) do (echo %%i)
Anthony Miller
  • 15,101
  • 28
  • 69
  • 98