Windows command prompt: how to get the count of all files in current directory?

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:

set count=0 & for %x in (*) do @(set /a count+=1 >nul)
echo %count%

That's assuming the command line. In a batch file you would do

@echo off
setlocal enableextensions
set count=0
for %%x in (*) do set /a count+=1
echo %count%
endlocal

which does things a little nicer. You can drop the >nul in a batch, since set /a won't display the result if run from a batch file—it does directly from the command line. Furthermore the % sign in the for 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:

  1. 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.

  2. Using find /c and try finding something that is always included in a file name. Obviously the dot (.) is a poor choice. Another answer suggests

    dir /a-d | find /c ":"
    

    which 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 on dir /b which cuts away all the non-filename stuff and avoids fencepost errors that way.

So the elegant variant would be:

dir /b /a-d | find /c /v ""

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

Counting the Number of Items in a Folder

Well, what do you know: it looks like the sun is finally coming out, which means it’s almost time for us to go. Before we do, however, let’s show you one last little trick with the Get-ChildItem cmdlet. Sometimes you don’t really need to know much about the files in a folder; all you really need to know is how many files (if any) can be found in a particular folder. Here’s how you can quickly count the number of files in a folder:

(Get-ChildItem C:\Scripts).Count

What are we doing here? We’re simply using Get-ChildItem to return a collection of all the items found in the folder C:\Scripts; because this is a collection, all we have to do is echo back the value of the Count property, which tells us the number of items in the collection. Note the use of parentheses: we enclose the Get-ChildItem command in parentheses to ensure that Windows PowerShell first grabs the collection and only then echoes back the value of the Count property for that collection.

And sure, you can include a filter when calling Get-ChildItem. Need to know how many .PS1 files are in the folder C:\Scripts? Okey-doke:

(Get-ChildItem C:\Scripts -filter "*.ps1").Count

FYI .. I googled "powershell count files".


dir /a-d | find "File(s)"

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.