dir Command
To count number of child directories in
C:\
, not counting sub-directories:
dir C:\ /A:D
To count number of child directories in C:\
and in sub-directories:
dir C:\ /S /A:D
To count number of files in C:\
, not counting sub-directories:
dir C:\ /A:-D
To count number of files in C:\
and in sub-directories, use:
dir C:\ /S /A:-D
To list files in a OneDrive folder:
dir /a
To list files one page at a time:
dir | more
The hyphen before D
means “not”, as in
“not directories”. In each case, the number
of files and directories is reported at the
end of the output, as "m File(s) n Dir(s)"
.
Note that every directory contains two
“alias” directories,
. (dot) and .. (dot dot).
These two alias directories are included
in the count that dir
reports. Thus,
dir %path%
will report “2 Dir(s)” when
%path%
is an empty directory. Furthermore,
if %path%
itself is a directory, then
dir %path%
does not include the %path%
itself in the directories count, but its
two child alias directories are counted.
Thus, the formula for the reported count
is:
ReportedCount = TrueCount + TrueCount * 2 + 2 = TrueCount * 3 + 2
Thus, to calculate the TrueCount from the ReportedCount, use:
TrueCount = (ReportedCount - 2) / 3