Counting Files and Subfolders Using the Command Prompt

Written by Eric Wyatt (last updated January 7, 2021)

3

In another article, we discussed how as we add more and more data to our computers. Over time, it is easy to lose track of how many items are stored in one folder. In that article, we discussed how to use PowerShell to quickly find the number of files and how to recursively search and see how many items are in any subfolders. While PowerShell is the more powerful command-line shell, some prefer using the regular old command line. Just as we did with PowerShell in the other article, we can use the power of command line to return the number of files and/or folders in a specific location. Follow these steps:

  1. Press theÊWindowsÊKey and type (without quotes) "CMD", and pressÊEnter. Windows opens a Command Prompt window.
  2. To search for the number of files within a directory, enter the following command. (Don't worry; I'll explain the command line in a moment.)
  3. dir "FilePath" /a:-d /s /b | find ":" /cÊ
    
  4. Press Enter. Command prompt will then return the number of files found within the directory you entered.

To find the number of subfolders within a directory enter the following command either in place of step 2 above or after step 3 and press Enter. (Again, I'll explain this command in a moment.)

dir "FilePath" /a :d /s /b | find ":" /cÊ

If you look at the two commands, they appear very similar. Let us look at what is happening here:

The dir command looks at the directory you specify in the "FilePath" location on your system. This ("FilePath") is the path to the directory or folder for which you want a count. Remember that the FilePath location needs to be contained within quote marks. A handy tip when it comes to entering your file path is once you type dir, followed by a space, click and drag the folder you are wanting information about into the Command Prompt window. Windows automatically places the file path with the accompanying quotes for you.

Next come the switches that control how the dir command does its work. The /a is used to specify which file attributes we want it to look at. In our instances we use either a ":d" or a ":-d". The ":d" tells dir to look only at directories (or subfolders) within the FilePath location. The use of ":-d", adding the dash in front of the "d", tells dir to pay attention to everything except the subfolders.

The /s switch makes the dir command recursive, meaning it will look inside each directory or subfolder within the "FilePath" location, not just the main folder. Finally, the /b switch reduces the amount of information returned by the dir command.

Next on the command line is a vertical bar (|), which is referred to as a "pipe." The pipe causes the output of the command that is just before it (dir) to be used as input for the command that immediately follows it (find). Thus, the ouput of the dir command will be used as input for the find command.

Which brings us to the find command. This command is used to find instances of one string within another. In this case, we are specifying that what follows the find command (":") should be found in the output produced by the dir command. Finally, the /c switch indicates that we want the find command to only return a count of how many lines of the dir output contain colons.

Knowing how to use the command prompt in this way can help you to quickly get the counts relating to the number of files or folders within a directory quickly and effortlessly.

 This tip (13819) applies to Windows 10.

Author Bio

Eric Wyatt

Eric Wyatt is a swell guy (or so his friends tell him). He is a formally trained designer and branding expert, bringing a wide range of skills to his Tips.Net articles. ...

MORE FROM ERIC

Lock Your Computer With Your Phone

Sometimes you have to get up quickly from your computer before you have time to lock it. With a paired phone and Dynamic ...

Discover More

Monitoring Data Usage

With Windows 10 you can easily see how your data is being used. Even if you are not on a metered data plan, it can be ...

Discover More

Illegal Windows Names

When it comes to files and folders and naming them in there are restrictions. Knowing what is restricted will help you ...

Discover More
More WindowsTips

Displaying the Command Prompt in Windows 8

Some commands require the use of the command prompt window. How you display that essential window in Windows 8 can be a ...

Discover More

Moving Files Using the Command Line

The MOVE command can be a timesaver over trying to do the similar sort of thing with File Explorer. You can move hundreds ...

Discover More

Switching Between Command Line and File Explorer

Sometimes you can be more effective in a command window, and other times you can be more effective using File Explorer. ...

Discover More
Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2021-01-04 20:48:16

Vince

Looks like several end marks that should be quotes in the cmd line descriptions on my computer turned into Ê character. Did not check it in any other browser. This was noticed in the EDGE browser,


2021-01-04 15:10:30

Glenn Case

While I can drag/drop a directory into the command prompt window, on my system it does not place the directory name inside quotes.

Another thing which might be noted is that if you want the current directory to be searched, replace the "FilePath" with a single period.


2021-01-04 06:27:00

Aussie--ii

What is the character "Ê" at the end of the command?
Commands worked well without it but gave "Invalid switch" with it.
Aussie--ii


Newest Tips