What command would you use in cmd.exe
to find the number of files in the current directory?
Is there a powershell option here?
Update: I was hoping to avoid dir
, as I know there are 10,000+ files in the current directory. Wanted to avoid the enumeration output to the cmd window. Thank you!
If you want to do it with
cmd
, then the following is the trivial way to do it:That's assuming the command line. In a batch file you would do
which does things a little nicer. You can drop the
>nul
in a batch, sinceset /a
won't display the result if run from a batch file—it does directly from the command line. Furthermore the%
sign in thefor
loop has to be doubled.I've seen quite a few instances where people try nifty tricks with
find /c
. Be very careful with those, as various things can break this.Common mistakes:
Using
find /c /v
and try finding something that is never included in a file name, such as::
. Won't. Work. Reliably. When the console window is set to raster fonts then you can get those character combination. I can include characters in a file name such as:
,?
, etc. in their full-width variants for example, which will then get converted to their normal ASCII counterparts which will break this. If you need an accurate count, then don't try this.Using
find /c
and try finding something that is always included in a file name. Obviously the dot (.
) is a poor choice. Another answer suggestswhich assumes several things about the user's locale, not all of which are guaranteed to be true (I've left a comment detailing the problems there) and returns one result too much.
Generally, you want to use
find
ondir /b
which cuts away all the non-filename stuff and avoids fencepost errors that way.So the elegant variant would be:
which will first output all file names, one line each. And then count all lines of that output which are not empty. Since the file name can't be empty (unless I'm missing something, but Unicode will not trip this up according to my tests).
Ask and ye shall receive: http://technet.microsoft.com/en-us/library/ee692796.aspx
FYI .. I googled "powershell count files".
A word of warning about using PowerShell for simple file operations like this - it is incredibly slow compared to cmd.exe, especially over network connections or when there are thousands of files in the directory. See this forum post for more info.
The fastest method I have found is to run the following in the PS console.
where $path is a local path or UNC share.
dir
gives you the total file count at the bottom.just count lines from the dir /B output (directories included):
dir /B | find /c /v "~~~"
for just count files (with no dirs):
dir /A-D /B | find /c /v "~~~"
Found this on the net:
I tested and it seems to work.
Just a caveat for those who might use Trevoke's PowerShell answer above (Windows command prompt: how to get the count of all files in current directory?): by design, echoing the Count property of a collection returned by Get-ChildItem can give misleading results if the value is 1 or 0. This is because the Count property is only available on arrays. If fewer than 2 items are returned by Get-ChildItem, the result is scalar, and .Count returns nothing. To get around this, explicitly cast it into an array, e.g., @(Get-ChildItem C:\Scripts).Count.
For more info, see http://connect.microsoft.com/PowerShell/feedback/details/354672/get-childitem-count-problem-with-one-file or http://www.vistax64.com/powershell/266379-get-childitem-count-not-working-one-file.html.
dir
returns the number of files and dirs at the bottomDIr will of course work
Without installing something on the server but having powershell on your workstation you could do:
that command will give you all the files (system and hidden included) and as previously mentioned if powershell is on the machine you can simply
Note that if you do not include the . you will get a count of all files and all directories. You will not include hidden and system files you need to add -force to count them all