How to List all Files in a Directory using CMD in Windows
Last Updated :
06 Mar, 2025
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.
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
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
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
How to Create a Batch File in Windows?
A batch file is a straightforward text document in any version of Windows with a set of instructions meant to be run by Windows' command-line interpreter (CMD) by maintaining all the functions. Batch files are a great tool for streamlining internally configured processes, automating tedious jobs, an
5 min read
How to Open File Explorer in Windows 11?
Short Preview!Here is a short preview of our active and speedy learner. Follow these steps to open File Explorer on your Windows PC. Method 1: Simply Type "File Explorer" in the search bar.Method 2: press the "Windows key + E."File Explorer will open, allowing you to manage files and folders in Wind
4 min read
How to use CMD for Python in Windows 10?
Using the Command Prompt (CMD) is an effective way to interact with Python on your Windows 10 machine. Whether you're writing scripts, testing code, or running programs, mastering CMD for Python in Windows 10 is crucial. This article will guide you on how to run Python in CMD, execute scripts, and t
4 min read
How to Hide a Folder in Windows?
If there is one thing that Windows is known for, then it is the customization ability that it brings to its users. This ability also extends to the folders one uses in Windows - primarily, the ability to hide them from peering eyes. In this article, we will be going through 2 methods to achieve Hidi
2 min read
How to Use Linux Commands in Windows with Cygwin
Cygwin is a collection of tools that provide a Linux Operating System's terminal look, feel, and some of its basic functionality for users of Windows OS who want to have a go over the basic commands of Linux without worrying about installing a Linux OS. To use this tool, first of all, we need to dow
3 min read