308

How do you iterate over each file in a directory with a .bat or .cmd file?

For simplicity please provide an answer that just echoes the filename or file path.

6 Answers 6

399

Command line usage:

for /f %f in ('dir /b c:\') do echo %f

Batch file usage:

for /f %%f in ('dir /b c:\') do echo %%f

Update: if the directory contains files with space in the names, you need to change the delimiter the for /f command is using. for example, you can use the pipe char.

for /f "delims=|" %%f in ('dir /b c:\') do echo %%f

Update 2: (quick one year and a half after the original answer :-)) If the directory name itself has a space in the name, you can use the usebackq option on the for:

for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f

And if you need to use output redirection or command piping, use the escape char (^):

for /f "usebackq delims=|" %%f in (`dir /b "c:\program files" ^| findstr /i microsoft`) do echo %%f
Sign up to request clarification or add additional context in comments.

25 Comments

At least for some commands, the file variable should be enclosed in double quotes.
The /f after FOR restricts it to files. Similarly, /d restricts to directories (folders) and /r instructs it to be recursive.
+1 This is a living answer, continuously attended to; many thanks.
Just realized you have to use back quotes... Instead of 'dir /b "c:\program files"' it should be `dir /b "c:\program files"`
Nice Job. Only suggestion is that you add quotation marks around %%f i.e.do echo "%%f`" If you don't have quotation marks, you will be trying to execute a command with a space in it. For example start "hello world.bat" is a valid command, but start hello world.bat is not.
|
142

Alternatively, use:

forfiles /s /m *.png /c "cmd /c echo @path"

The forfiles command is available in Windows Vista and up.

4 Comments

it was difficult using paths with spaces in the 2nd cmd shell. In the end I adjusted %PATH% env var to get the job done. But thanks. helpful
works very well for example: forfiles /s /m *.bak /c "cmd /c 7za.exe a @path.7z @path"
Why do you call another external program, cmd here? I have cmd restricted (however, batch files are working and powershell is also here).
99

Easiest method:

From Command Line, use:

for %f in (*.*) do echo %f

From a Batch File (double up the % percent signs):

for %%f in (*.*) do echo %%f

From a Batch File with folder specified as 1st parameter:

for %%f in (%1\*.*) do echo %%f

4 Comments

not just easiest, but also significantly more elegant compared to evaluating the dir /B output with for.
Add a /r before the %f to recurse subdirectories
Why is this not the accepted answer
What I like with this solution is that the %f variable contains the file full path (directory and file name), which is not the case for some other solutions.
39

Use

for /r path %%var in (*.*) do some_command %%var

with:

  • path being the starting path.
  • %%var being some identifier.
  • *.* being a filemask OR the contents of a variable.
  • some_command being the command to execute with the path and var concatenated as parameters.

3 Comments

I get the error: %%var was unexpected at this time. Can you give an exact example? I tried a bunch of variations of for /r . %%var in (*.*) do echo %%var
For some strange reason, the variable name is restricted to one character.
Can we use different smileys for different results?
10

Another way:

for %f in (*.mp4) do call ffmpeg -i "%~f" -vcodec copy -acodec copy "%~nf.avi"

2 Comments

what "~nf" stands for?
@JudgeDredd "%~nf" stands for name of the file without extension, where f is the name of the variable specified in for part. Docs: learn.microsoft.com/en-us/previous-versions/windows/it-pro/…
0

I had some malware that marked all files in a directory as hidden/system/readonly. If anyone else finds themselves in this situation, cd into the directory and run for /f "delims=|" %f in ('forfiles') do attrib -s -h -r %f.

2 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
Your answer doesn't really answer the question very well if they don't understand the context of the line given. You didn't need to do this anyway as ATTRIB has command line flags to run through sub-directories and process all files. ATTRIB -S -H -R /S /D *.* (/S Processes files in sub-directories, /D processes folders too)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.