284

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.

Ross Ridge
  • 35,323
  • 6
  • 64
  • 105
Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619

6 Answers6

361

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
yoel halb
  • 10,405
  • 3
  • 48
  • 46
Franci Penov
  • 71,783
  • 15
  • 124
  • 160
  • I tried to enumerate the files in a current directory with this command [ for /f %%f in ('dir /b .') do echo %%f ].. Unfortunately files with spaces had their name printed up to their first space.. ? Thanks – lb. Dec 07 '09 at 00:07
  • 3
    At least for some commands, the file variable should be enclosed in double quotes. – Jirka Jan 03 '11 at 14:45
  • This is also useful: How to do multiple things to each file in a directory with a batch script http://stackoverflow.com/questions/2304867/how-to-do-multiple-things-to-each-file-in-a-directory-with-a-batch-script?lq=1 – Snekse Jun 25 '12 at 02:24
  • 1
    Why is the `/f` needed after the `for`? According to the help docs, the `/f` flag opens and reads each file. Is that needed to echo or rename files? – Snekse Jun 25 '12 at 02:33
  • 19
    The `/f` after `FOR` restricts it to files. Similarly, `/d` restricts to directories (folders) and `/r` instructs it to be recursive. –  Aug 07 '12 at 15:15
  • 4
    +1 This is a living answer, continuously attended to; many thanks. – Sabuncu Jul 20 '14 at 20:24
  • Why not just use "tokens=*" instead of "delims=|" for spaces? – Jared Allard Jul 21 '14 at 20:22
  • 1
    I ran this: `C:\Program Files>for /f "usebackq delims=|" %%f in ('dir /b "c:\program files"') do echo %%f` but I always get the output message "%%f was unexpected at this time.". Why? What's wrong? – Dai Apr 05 '15 at 05:49
  • %% is for use in scripts. if you are just running a command in the cmd, use %f instead – meteorainer Jul 07 '15 at 19:25
  • Hi, I think you should complement your answer by adding the recommendation to change dir (cd) into the files folder. This is due to %f not having full path, and as so, operations in the files aren't accessed from anywhere. – Pedro Reis Oct 30 '15 at 08:07
  • That is true. `dir /b` returns only the file name. It's trivial to use concatenation to add the directory name in the operation after `do` though. No need to change to the directory. – Franci Penov Oct 30 '15 at 19:02
  • what if you want to a batch file to iterate over .dwg files in a directory - where both the directory and the filename have spaces. This is my attempt: -----> for /f "usebackq delims=|" %%f in ('dir /b "c:\program files\*.DWG"') do echo %%f -------> will that work? – BKSpurgeon Feb 04 '16 at 01:14
  • `for /f "usebackq delims=|" %%f in ('dir /b "c:\program files\*.DWG"') do echo %%f` -> this should work, though I don't have a Windows machine right now to verify. – Franci Penov Feb 04 '16 at 18:36
  • When I run the Update2 script from a batch file, it only echoes `dir /b "c:\program files"` not the actual list of files :( – endavid Aug 08 '16 at 13:16
  • 4
    Just realized you have to use back quotes... Instead of `'dir /b "c:\program files"'` it should be `\`dir /b "c:\program files"\`` – endavid Aug 08 '16 at 13:24
  • @endavid: I edited the answer to fix the back quotes. Thanks for catching that. I was having the same problem. – GuitarPicker Sep 15 '16 at 20:37
  • 1
    @GuitarPicker: The back quotes are only used if you have "usebackq" as an option, otherwise you use single quotes. So it works for the examples in Edit 2 but breaks the original answer. – DavidR May 09 '17 at 21:00
  • It was fun to watch over the years how people would fight whether it should be backquotes or straight single quotes. :-) – Franci Penov May 10 '17 at 05:34
  • ` => The system cannot find the file `dir. – Quentin 2 Jun 12 '18 at 08:41
  • For full paths in subdirs use 'dir /b /s' – Quentin 2 Jun 12 '18 at 08:42
  • BTW For a path with spaces you can just surround the path with double quotes, and then there is no need for backticks – yoel halb May 08 '19 at 08:33
  • 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. – Jacob Ward Mar 07 '21 at 05:13
119

Alternatively, use:

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

The forfiles command is available in Windows Vista and up.

Paul Houx
  • 1,717
  • 1
  • 13
  • 14
  • 1
    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 – twobob May 01 '17 at 20:09
  • works very well for example: forfiles /s /m *.bak /c "cmd /c 7za.exe a @path.7z @path" – Bludau Media Aug 12 '20 at 10:47
79

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
Community
  • 1
  • 1
Gordon Bell
  • 12,283
  • 3
  • 41
  • 63
36

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.
mstrobl
  • 2,289
  • 14
  • 16
8

Another way:

for %f in (*.mp4) do call ffmpeg -i "%~f" -vcodec copy -acodec copy "%~nf.avi"
Juri Noga
  • 4,205
  • 7
  • 32
  • 45
thistleknot
  • 714
  • 12
  • 28
  • what "~nf" stands for? – player0 Apr 25 '18 at 15:24
  • @JudgeDredd `"%~nf"` stands for name of the file without extension, where `f` is the name of the variable specified in `for` part. Docs: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10) – Slava Apr 27 '18 at 17:03
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.

Anon
  • 21
  • 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. – SherylHohman Sep 06 '20 at 07:50