Open In App

How to List all Files in a Directory using CMD in Windows

Last Updated : 06 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Struggling to find or organize files buried in folders on your Windows PC? While File Explorer offers a visual way to browse, the Command Prompt (CMD) provides a faster, more powerful method to list, sort, and manage files, especially when dealing with large directories or automating tasks.

In this quick guide on how to list all files in a directory using CMD in Windows, you will explore how to use simple CMD commands like dir to instantly display every file and subfolder in any directory. We’ll break down the syntax, explain how to filter results (by name, date, or type), and share tricks to export lists for backups.

Prerequisites to Access Directory in Win

  • Windows PC (11, 10, 8 or 7)
  • Administrator Rights
  • Familiar with Command Prompt actions (or knowing how to open it, i.e. Win + R & type CMD)

What Command Can Be Used to List Files

The "dir" command is fundamental in the Windows Command Prompt, which is used to list the contents of a directory. Here's a bit more detail on how it works.

1. Basic Usage

  • When you type dir and press Enter in the Command Prompt, it lists all the files and directories in the current directory.
  • By default, it displays the file name, size, and modification date and time.

2. Options

  • /A: This option displays files with specified attributes. For example, /A: H displays hidden files.
  • /B: Uses a bare format with no heading information or summary. It simply lists the names of files and directories.
  • /O: Specifies the order in which files are sorted. For instance, /O: N sorts files by name.
  • /S: Displays files in the specified directory and all subdirectories.
  • /P: Pauses after each screenful of information.
  • /W: Uses wide list format, displaying as many as five file names in each row.

3. Example Usage

  • dir /A: Lists all files including hidden files in the current directory.
  • dir /B: Displays a bare list of files and directories without any additional information.
  • dir /O:N: Sorts files by name.
  • dir /S: Lists files in the specified directory and all subdirectories.

How to List All Files in a Directory

Now, we will be discussing 4 basic steps to list all files in a directory using the Command Prompt. Let's check them out:

Step 1: Open Command Prompt

  • First things first, let's open the Command Prompt. You can do this by searching for "Command Prompt" in the Start menu or by pressing the Win + R keys, typing "cmd" in the "Run" dialog, and hitting Enter.
list-all-files-in-a-directory
Open Command Prompt

Step 2: Navigate to the Directory

  • Once you have the Command Prompt open, you need to navigate to the directory whose files you want to list. To do this, use the cd command followed by the path of the directory. For example, if you want to list files in a directory named "Documents" located in your user folder, you would type:
cd Documents

Press Enter after typing the command to change the directory.

Step 3: List Files

  • Now that you're in the desired directory, it's time to list all the files it contains. To do this, simply type the "dir" command and press Enter. This command displays a list of files and directories in the current directory.
List-Files
List Files

Step 4: Additional Options (Optional)

  • If you want to customize the way files are listed, you can use various options with the dir command. Here are some common options:
/A: Displays files with specified attributes.
/B: Uses bare format (no heading information or summary).
/O: Specifies the order in which files are sorted.
/S: Displays files in specified directory and all subdirectories.

For example, if you want to list all files including hidden files in bare format, you would type:

dir /A /B

Use Cases for Listing Files in CMD

  • IT Audits: Export a list of all .exe files in a directory for compliance checks.
  • Scripting Backups: Automate file logging with a batch script.
  • Troubleshooting: Identify missing or corrupted files in large folders.

CMD vs PowerShell: Which One to Use

TaskCMD CommandPowerShell Equivalent
List filesdirGet-ChildItem
Filter by datedir /odGet-ChildItem | Sort LastWriteTime
Export to text filedir > output.txtGet-ChildItem > output.txt

Conclusion

Mastering the dir command in CMD lets you efficiently manage files, automate tasks and troubleshoot Windows system within a few steps. By following the outline steps, you can use the Command Prompt to organize your workspace or troubleshooting, without replying on graphical interfaces. Whether you're an IT expert, a developer (for scripting batch jobs) or a casual user organizing personal documents, the ability to list files in a directory using CMD is a must-know job that everyone should know.

Also Read


Next Article
Article Tags :

Similar Reads