Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I’m struggling with one of the most frustrating bugs I’ve ever come across.

Bug description:

  The for command of the command-interpreter cannot see hidden files.

Reproduction steps:

  1. Create a temporary directory
  2. Create a few files
  3. Assign a variety of attributes to the files (including hidden)
  4. Use a command like for %i in (*) do echo "%i"

Expected results:

   All files are processed in the for loop either by default or though a switch.

Actual results:

  • Files with any attribute other than hidden are processed; files flagged as hidden are skipped
  • There is no switch to the for command to allow it to process hidden files


   There is no way to process all files from the command-prompt.


   How the heck can hidden files be processed from the command-prompt or batch-files (at least in Windows if not DOS)?

share|improve this question
up vote 4 down vote accepted

Here is one (ugly, unpleasant, non-ideal) work-around for files:

for /f "tokens=* delims=" %i in ('dir /b/a-d *') do echo "%i"

And a version for folders (analogous to for /d…):

for /f "tokens=* delims=" %i in ('dir /b/ad  *') do echo "%i"

And a version for both (no for analog, so this is something of a benefit):

for /f "tokens=* delims=" %i in ('dir /b/a   *') do echo "%i"

Recursive version for files (for… /r):

for /f "tokens=* delims=" %i in ('dir /b/s/a-d *') do echo "%i"

And folders (for /r /d…):

for /f "tokens=* delims=" %i in ('dir /b/s/ad  *') do echo "%i"

And both

for /f "tokens=* delims=" %i in ('dir /b/s/a   *') do echo "%i"

This works more or less, but definitely has its issues (especially for large sets of files or folders). Hopefully there is a better solution or Microsoft could fix/improve this in a patch.

share|improve this answer
With tokens=* you can omit the delims= option, and the * is redundant. Also dir /b/a will list directories as well as files. If you want files only, you need dir /b/a:-d. And since this behavior of the Windows command line hasn't changed in, like, 20 years, it's probably not going to change now. Don't like it? Use VBScript or PowerShell instead. – Ansgar Wiechers Sep 16 '12 at 20:07
@AnsgarWiechers, thanks for the tips. I don’t usually use /f other than to process the lines in a text file (e.g., for /f "delims=" %i in (list.txt)…), so I don’t know the extra options that well. You’re right about /a listing directories; I don’t know why my earlier test did not list them. – Synetech Sep 16 '12 at 21:02
@Synetech, hmm, I improved the answer and an up-vote got rescinded. I guess that’ll learn me to make things better from now on. sigh – Synetech Sep 16 '12 at 21:20
I just had to resort to this for a new batch-file I wrote and to be honest, it wasn’t that bad. It performs well enough and was simple to remember and use (I left the tokens and delims options out altogether for this one). I’m going to accept this for now because it retains the for syntax, so nothing has to be rewritten, and it “just works”. – Synetech Sep 22 '12 at 23:35
I’ve been using the /f switch to use a command for the loop expression a lot over the past months and to be honest, I am really getting used to it. It still has the drawback that it must first enumerate all files/folders to build a list before even starting the command, but it also has some benefits like using dir to access hidden/system files/folders and building the list before processing avoids re-processing items that are modified; a regular for loop enumerates in situ, so it can enter an infinite loop since modified objects can appear as new ones to be (re-)processed. – Synetech May 13 '13 at 4:10

You could use forfiles instead of for.

The syntax is quite different, but it has more functionalities (e.g., method to natively access the filename, the extension, the filesize and timestamp) and it processes all files by default.

For example, instead of

for %i in (*) do echo "%i"

you'd use

forfiles /c "cmd /c echo @file"

or simply


since "cmd /c echo @file" is the default value for the command switch.

share|improve this answer
Not bad. It uses a different syntax and isn’t available in XP, but it’s good to have an option. (I actually considered a Unix port, but while ls.exe is useful, the for command is built into the shell like it is in Windows. I suppose one could use a Unix shell port, but then that wouldn’t be the Windows command-prompt.) – Synetech Sep 16 '12 at 19:55
You could pipe the output of ls to xargs or simply use find. Both are available for Windows. – Dennis Sep 16 '12 at 21:09
Hmm, interesting suggestion. The problem is that running external commands like that would severely impact the performance, especially when there’s lots of files/folders (at least as compared to the built-in for command). – Synetech Sep 16 '12 at 21:21

You must log in to answer this question.

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