For Loop in DOS Batch File

FOR %%c in (C:\Windows\*.*) DO (echo file %%c)
You can implement a FOR loop to act on one or more files returned from the IN clause. We originally found this in order to GPG decrypt a file using wildcards (where you don't know exactly the entire file name, i.e.: Test_File_??????.txt, where ?????? = the current time in HHMMSS format). Since we won't know the time the file was generated, we need to use wildcards. And as a result of GPG not handling wildcards, this is the perfect solution. Thought I would share this revelation. :-)
Sample Output
file C:\Windows\bfsvc.exe
file C:\Windows\bootstat.dat
file C:\Windows\DtcInstall.log
file C:\Windows\EnterpriseN.xml
file C:\Windows\explorer.exe
file C:\Windows\fveupdate.exe
file C:\Windows\HelpPane.exe
file C:\Windows\hh.exe
file C:\Windows\HPMProp.INI
file C:\Windows\IE9_main.log
file C:\Windows\iis7.log
file C:\Windows\ISMIF32.dll

Press any key to continue . . .

By: jmcclosk
2013-01-31 15:19:54

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: