How to List all Files in a Directory using CMD in Windows
Last Updated :
21 Jun, 2025
Are your files buried deep in folders and hard to find? While File Explorer is good for browsing visually, the Windows Command Prompt (CMD) offers a faster and more powerful way to manage and list files. Whether you're troubleshooting, creating backup lists, or writing scripts, the dir
command gives you complete control.
In this step-by-step guide, you'll learn how to list all files in a directory using CMD, filter results, and even export the file list.
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 is the dir Command in CMD?
The "dir" command is a fundamental in the Windows Command Prompt, which is used to list the contents of a directory, including files and subfolders. This command is especially useful when working with large folders, hidden files, or when automating tasks. Here's a closer look at how it works.
Basic Syntax: dir [options] [path]
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.
Open Command PromptStep 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 FilesStep 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
Task | CMD Command | PowerShell Equivalent |
---|
List files | dir | Get-ChildItem |
Filter by date | dir /od | Get-ChildItem | Sort LastWriteTime |
Export to text file | dir > output.txt | Get-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
Similar Reads
How to List all Files in a Directory using CMD Are your files buried deep in folders and hard to find? While File Explorer is good for browsing visually, the Windows Command Prompt (CMD) offers a faster and more powerful way to manage and list files. Whether you're troubleshooting, creating backup lists, or writing scripts, the dir command gives
5 min read
How to print all files within a directory using Python? The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to
3 min read
How to Copy Files from One Directory to Another Using CMD Have you ever needed to copy a bunch of files from one folder to another without clicking through endless windows? Or perhaps youâre looking to streamline a repetitive task with a single line of text? If so, the Command Prompt (CMD) on Windows is here to save the day! Copying files between directori
5 min read
How to Merge/Hide One File into Another Using CMD in Windows? Merging or hiding files within other files using Command Prompt in Windows is a clever technique for managing and protecting your data. Whether you want to consolidate multiple files into one or conceal sensitive information, CMD offers powerful commands to achieve this seamlessly. Learning how to h
4 min read
How to Create an Infinite Loop in Windows Batch File? Windows batch files are a powerful tool for automating repetitive tasks, managing system operations, and streamlining workflows. But what happens when you want to create a process that runs indefinitely?In this blog post, we'll walk you through the steps to create an infinite loop in a Windows batch
4 min read