Counting Files and Subfolders Using the Command Prompt

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

Keeping Track of Time with Timers

Often it is necessary to be notified when a set amount of time has gone by. With the Timer tab of the Alarms & Clock app ...

Discover More

Getting Started with Continue on PC

In todays on-the-go lifestyle, we tend to have multiple electronic devices. Easily being able to connect our devices can ...

Discover More

Finding Your Wi-Fi Password

Connecting devices to your wireless network is generally easy, you select the network and enter the password. But, what ...

Discover More
More WindowsTips

Command Prompt's New Tricks

Command Prompt has been around for a while in various versions of Windows. With Windows 10 the Command Prompt window has ...

Discover More

Quickly Finding Your IP Address

IP addresses identify your device on IP-governed networks. If you need to find your IP address quickly, you can use the ...

Discover More

Using the Sort Command

Sorting data is a common task even of end users. Fortunately, The Windows command line provides us with a Sort utility so ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

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}] 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 5 - 1?

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
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.