First, dir/b/s c:\*.*
is of invalid syntax. It works because of Windows command processor automatically corrects the command line. Correct would be:
dir /b /s C:\*.*
Or 100% valid shorter:
dir /b /s C:\*
Or 100% valid shortest:
dir /b /s C:\
The general syntax on Windows command line is:
command/executableSPACEargument1SPACE"argument 2"SPACE/option
No space between command dir
and its first argument /b
is not 100% correct syntax. It is in general not good in no scripting and programming language on writing code depending on automatic correction of the syntax by the application interpreting the code. I see this daily on visiting lots of websites with an old browser where the old browser fails to display a webpage right just because of incorrect code in one of the files of the webpage which are displayed fine by latest browsers because of their excessive auto-detection and auto-correction of syntax errors caused by the people writing the files of the webpage.
The execution of the DIR command in the batch file is slower because of FOR with option /F
and a set in '...' starts in background using %CompSpec% /C
a new command process for execution of the DIR command line. Everything finally output to handle STDOUT is captured by FOR and processed after started cmd.exe
terminated itself.
FOR with option /F
ignores all empty lines on processing captured output of additional command process. This behavior cannot be changed with options.
FOR with option /F
splits up each line into substrings (tokens) using normal space and horizontal tab character as string delimiters. The string splitting behavior can be controlled by using option delims=
whereby using this option with specifying no characters turns off the string splitting behavior.
FOR with option /F
assigns to specified loop variable by default only the first space/tab separated substring. This behavior can be controlled with option tokens=
. The usage of "tokens=*"
results in removing all leading spaces/tabs and assign rest of captured line to the specified loop variable.
FOR with option /F
ignores also all lines starting with a semicolon by default. This behavior can be controlled by option eol=
(end of line).
So what happens on execution of this command line:
for /f "tokens=*" %%a in ('dir/b/s c:\*.*') do ( echo "%%a")
- FOR starts in background a command process which executes the command DIR.
- DIR searches for non-hidden files and directories on drive C: and all its non-hidden subdirectories and outputs their names with full path to handle STDOUT of the background command process captured by FOR.
- In background started
cmd.exe
terminates itself after DIR finished.
- FOR processes the captured lines.
- There are no empty lines because
dir /B
outputs no empty lines.
- There are no lines starting with spaces/tabs because
dir /B /S
results in output of file and directory names with full path starting with drive C:
in this case. A file or directory name without full path could begin with one or more spaces.
- There are no lines starting with
;
also because of dir /B /S
. A file or directory name can have a semicolon as first character, but not on being output with full path.
- FOR runs the command ECHO for each string assigned to loop variable
a
.
Better would be the command line:
for /F "eol=| delims=" %%I in ('dir /B /S C:\ 2^>nul') do echo "%%I"
This command line would work also on DIR option /S
not used resulting in output of just file/folder name without path even for files/folders starting with a semicolon or a space character. The end of line option is defined with vertical bar because of no file/folder name can contain |
according to the Microsoft documentation Naming Files, Paths, and Namespaces.
It is advisable to run a command in a command prompt window with /?
as first and only argument to get displayed the help/documentation for this command before using it. Try it out with for /?
and dir /?
in a command prompt window and run also cmd /?
because of this executable processes a batch file.
There is the executable %SystemRoot%\System32\robocopy.exe
for copying an entire directory tree or deprecated older executable %SystemRoot%\System32\xcopy.exe
. See Windows Commands and SS64.com - A-Z index of the Windows CMD command line for documentation of these two external commands in addition to running them in a command prompt window with /?
for a brief help.