Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

11 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
+1 That was exactly what I was looking for. Thank you JR! –  p.campbell Feb 10 '10 at 1:18
add comment

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

share|improve this answer
    
You need to add a filespec to make the count actually count files, as per the second example –  Jim B Feb 8 '10 at 17:58
2  
Just to be clear (Get-ChildItem C:\Scripts).Count will NOT return the number of files in a folder it will return the number of OBJECTS in a folder. You must add a filter or file specification in order to get it to count files. try (gci).count and dir from the root of your C: drive and look at the difference in numbers –  Jim B Feb 8 '10 at 19:49
1  
From that same link, if you want to count files in the subfolders too use: (Get-ChildItem -recurse -filter "*.ps1").Count –  Jugglingnutcase Jun 29 '12 at 14:25
    
Doesn't work if there is exactly one file, have to do something like this: $filecount = @(get-childitem $directory -filter ".").Count. From what I read elsewhere, it's not an array if there is one file. –  NealWalters Jul 5 '12 at 21:10
add comment

dir gives you the total file count at the bottom.

share|improve this answer
    
1 Up mate, you beat me to this one :) –  dyasny Feb 8 '10 at 17:15
    
and to avoid the cost of watching all the lines scroll through the cmd window... minimize it, it'll work a LOT faster. –  gbjbaanb Feb 8 '10 at 18:31
    
or just dir > file .. ahh I miss POSIX –  lorenzog Feb 8 '10 at 23:28
    
@lorenz: Any reason why dir > file shouldn't work in Windows? Output redirection isn't exactly magic, you know. –  Јοеу Feb 9 '10 at 17:12
    
@johannes: it does work, in fact! I miss POSIX because you could "just" do dir | grep (something) to avoid the slow on-screen write that slows things down (and minimizing it altogether). –  lorenzog Feb 10 '10 at 9:13
show 2 more comments
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.

share|improve this answer
add comment

Found this on the net:

dir /a-d | find /c ":" > NUMfiles.###
set /p count=<NUMfiles.###
echo %count%

I tested and it seems to work.

share|improve this answer
    
“Seems to” is the correct word here. (1) This is off by one in any case, since the dir command includes a line with a colon in it which is not a file. (2) This relies on the time format to include a colon, which doesn't necessarily have to be so. (3) It relies on the number format not including a colon, which also doesn't necessarily have to be so. Besides, going through a file here is unnecessary and won't work if you don't have write access—bad idea. –  Јοеу Feb 9 '10 at 17:10
add comment

Just a caveat for those who might use Trevoke's PowerShell answer above (http://serverfault.com/questions/110725/windows-command-prompt-how-to-get-the-count-of-all-files-in-current-directory/110742#110742): 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.

share|improve this answer
add comment

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 "~~~"

share|improve this answer
    
This returns files and directory count as a total –  TheCleaner Dec 3 '12 at 15:05
add comment

dir returns the number of files and dirs at the bottom

share|improve this answer
add comment

DIr will of course work

Without installing something on the server but having powershell on your workstation you could do:

(gwmi -computer theserver -query "select *from cim_datafile where path = '\\'").count

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

(GCI c:\*.*).count

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

share|improve this answer
add comment

If I only needed this once I'd just use dir, otherwise I'd consider rolling my own, probably as a C# app because that's what I would be most familiar with (although VBScript or PowerShell would also be viable).

(By the way - it's not DOS if you're using cmd.exe)

share|improve this answer
add comment

I believe you can use attrib to get the file count:

attrib.exe /s /D *.*|find /c /v "" >> filecount.txt

This will create a file called filecount.txt in the current directory with the number of files and folders in the current directory (including the newly created file). You can change the

*.*

to

<full_path>\*.*

in order to get the file count of a particular directory. Remove the "/D" option if you don't want to count folders. The "/s" option processes files in all directories in the specified path. So remove it if you only want files in the specified path and don't want to include files in sub folders.

So for example if you want to find out how many files are in your C:\ drive and not any subfolder you would use the command:

attrib.exe C:\*.* | find /c /v "" >> filecount.txt
share|improve this answer
    
Using files for results is not the smartest idea. What do you do if the current directory is not writable by you? –  Јοеу Feb 9 '10 at 17:33
    
@Johannes Use a directory you do have write access to? e.g. pipe the output to C:\MyWritableDir\filecount.txt –  smoak Feb 9 '10 at 18:01
    
Still unnecessary. –  Јοеу Feb 9 '10 at 21:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.