JSI Tip 6478. Your FOR commands appears to hang when processing a large directory structure?

If your batch file contains a FOR command that is parsing a DIR /S command, such as:

for /f "Tokens=*" %%a in ('dir c:\ /b /s') do @echo %%a

the command may appear hang before returning any output.

The entire directory structure is parsed before the first line of output is returned.

To demonstrate this behavior, open a CMD prompt and type:

for /f "Tokens=*" %a in ('dir %systemroot% /b') do @echo %a

The output should be promptly returned. Now add the /S parameter by typing:

for /f "Tokens=*" %a in ('dir %systemroot% /b /s') do @echo %a

Notice the sizeable delay.

NOTE: The FOR command will exhibit this same behavior when processing the contents of a large file.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.