Windows – FOR Command Cannot See Hidden Files

command linewindows

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

Implications:

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

Question:

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

Best Answer

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

forfiles

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

Related Question