28

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!

blee
  • 245
  • 4
  • 18
p.campbell
  • 4,407
  • 6
  • 41
  • 51

13 Answers13

32

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

Joey
  • 1,853
  • 11
  • 13
  • 1
    +1 That was exactly what I was looking for. Thank you JR! – p.campbell Feb 10 '10 at 01:18
  • Small tweak to make the 1st version all one line: (set count=0 & for %x in (*) do @(set /a count+=1 >nul)) && echo %count% – Jason Massey Aug 29 '16 at 16:31
  • +1 for `dir /b /a-d | find /c /v ""` – Arvo Bowen Sep 08 '17 at 13:48
  • This solution doesn't work well when there are millions of files. It runs "forever" - a better solution is posted below that runs "in seconds" using PS and [System.IO.Directory]::GetFiles($path).Count – ripvlan Sep 23 '18 at 07:52
  • @jason, that would probably fail since `%count%` is expanded at parse time. – Joey Sep 23 '18 at 09:23
  • @ripvlan: Back then "Windows Command Line" basically meant `cmd`. I can't test right now, but the `for`-based variant shouldn't be needlessly slow. – Joey Sep 23 '18 at 09:25
  • @Joey I tried both the cmdline and batch.cmd version of your post on a folder containing 3.2 million files. It's been running for over 7 minutes and still hasn't completed. Where-as the PS script took 3 seconds. – ripvlan Sep 24 '18 at 14:56
18

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

Trevoke
  • 419
  • 3
  • 12
  • 1
    You need to add a filespec to make the count actually count files, as per the second example – Jim B Feb 08 '10 at 17:58
  • 3
    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 08 '10 at 19:49
  • 4
    From that same link, if you want to count files in the subfolders too use: `(Get-ChildItem -recurse -filter "*.ps1").Count` – jrsconfitto Jun 29 '12 at 14:25
  • 1
    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 05 '12 at 21:10
  • @NealWalters: This was correct until PowerShell v3. Since then it doesn't matter. – Joey Jun 30 '15 at 05:09
  • This solution doesn't work well when there are millions of files. It runs "forever" -- look below for a better answer that counts files "in seconds" using [System.IO.Directory]::GetFiles($path).Count – ripvlan Sep 23 '18 at 07:51
11
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.

rossnz
  • 659
  • 4
  • 7
10

The fastest method I have found is to run the following in the PS console.

[System.IO.Directory]::GetFiles($path).Count

where $path is a local path or UNC share.

p.campbell
  • 4,407
  • 6
  • 41
  • 51
John Brooks
  • 111
  • 1
  • 2
  • 2
    Look at that. A valid awesome answer appeared almost 7.5 years after the question was asked! – p.campbell May 30 '17 at 18:14
  • Worked on a Virtual Server in a directory with over 2.1 million files to return the count in about 10 seconds for me. This should be the correct answer for sure. – DarrenMB Jun 14 '17 at 00:04
  • This really is the best answer. I had a folder with 4.5 million files in it and this method counted them in several seconds. Methods in other proposed answers ran for several minutes before I gave up on them. – ripvlan Sep 23 '18 at 07:48
7

dir gives you the total file count at the bottom.

lorenzog
  • 2,799
  • 3
  • 20
  • 24
3

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

Garcia
  • 31
  • 2
2

Found this on the net:

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

I tested and it seems to work.

Kelsey
  • 143
  • 8
  • “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. – Joey Feb 09 '10 at 17:10
  • How to make it without creating file "NUMfiles.###" ? – NickUnuchek Jun 12 '15 at 09:29
2

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.

Heather M.
  • 21
  • 1
0

dir returns the number of files and dirs at the bottom

dyasny
  • 18,802
  • 6
  • 49
  • 64
0

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

Jim B
  • 24,081
  • 4
  • 36
  • 60
0

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)

Maximus Minimus
  • 8,987
  • 2
  • 23
  • 36
0

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
smoak
  • 646
  • 2
  • 7
  • 13
-2

I'm doing something similar and this works with any number of files instantly.

Setlocal enabledelayedexpansion

Dir /A | find "File(s)" >tmp

For /F "tokens=1" %%i in (tmp) do set n=%%i

Del tmp

If !n! GEQ 10 echo Too many files in directory.
  • Why post an useless (and flawed) answer to a 6-year-old question with already lots of (good) anwers? – Massimo Mar 26 '16 at 23:35
  • @Massimo because I was not satisfied with the answers above and wanted a more elegant solution. Why stand in the way of the pursuit of knowledge which is not limited by time? – Aaron Lowe Mar 27 '16 at 23:36
  • Urgh! The replies look horrible. Edited solution into answer. No need to thank me I love negative rep for helping others :-P – Aaron Lowe Mar 27 '16 at 23:50
  • There's really no reason to use a temp file to hold some output; also, using `find` is not a good idea. See the accepted answer for details. – Massimo Mar 27 '16 at 23:56
  • @Massimo I answered your question. You owe me an answer to mine. If you can show me how to avoid using the temp file I'd appreciate that. I already read all the answers which is why I posted my own. You are assuming that because an answer is accepted that new answers can't be found. It's not just for the OP but for everyone looking for the same solution. – Aaron Lowe Mar 28 '16 at 00:00
  • @Massimo and if you had read my reply to you you would have seen I said I read the other answers. – Aaron Lowe Mar 28 '16 at 00:04
  • @Massimo and now I am confused because the accepted answer recommends using Find. Found a better answer than the one I posted that I somehow missed before so I accept your -1 for that reason but really you shouldn't harass beginners for trying. I was a few seconds away from reporting you lol – Aaron Lowe Mar 28 '16 at 00:18
  • Have you actually ***read*** the accepted answer? It doesn't use temp files and it doesn't use `find`. It also *warns* about using `find`, because it can fail in multiple ways and should thus be avoided. – Massimo Mar 28 '16 at 00:27
  • Your answer adds nothing useful to this question (which, again, not only is ***six years old***, but already has a perfectly good answer). Hence the -1. – Massimo Mar 28 '16 at 00:28
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/37606/discussion-between-aaron-lowe-and-massimo). – Aaron Lowe Mar 28 '16 at 01:28
  • Moved to dicussion. Plus the answer I thought was better doesn't work if there are no files in the directory. So I'm using my "flawed", "pointless" and "useless" answer as it is the best one. – Aaron Lowe Mar 28 '16 at 01:40
  • Do as you wish. Bye. – Massimo Mar 28 '16 at 11:08