Counting Files and Subfolders Using the Command Prompt

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


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


Getting Cortana to Listen to You

Talking to your computer can save you time and help get you the information you need faster. With the proper setup you ...

Discover More

Drag to Install Fonts

Installing fonts within Windows 10 is now quicker and easier. By using the new drag-and-drop install method, your fonts ...

Discover More

Creating a QR Code for a Web Page

QR codes allow for data to be scanned quickly and actions to take place. Use Edge Chromium to create a QR code to open a ...

Discover More
More WindowsTips

Listing Folder and Directory Content Using Command Prompt

Using Command Prompt to generate file listing the contents of a directory is quick and easy. Here's how to do it.

Discover More

Introduction to Regular Expressions

Regular expressions are usually the domain of computer programmers. There are times, though, when an end-user might find ...

Discover More

Clearing the Address Resolution Protocol Cache

Corrupt or unwanted Address Resolution Protocol (ARP) cache entries can cause connectivity errors. Here is how to flush ...

Discover More

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 3 + 5?

2021-01-04 20:48:16


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


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

Newest Tips