Command line script to generate a text list of all the Windows files contained in a folder.
How to create a text list of all Windows files contained in a folder with the cmd command prompt? It’s actually quite simple and involves combining two MS-DOS scripts. I.e., redirecting the output of the dir batch command to a text file. This tutorial will guide you through the steps, so you can quickly generate a file list from a folder in Windows 10 and beyond.
Table of Contents
1. Create a text file with a list of all files in a folder
If you want to export folder names to a text file using cmd, start with these steps:
- Open the Windows command prompt
- Navigate to the folder you need to list
- Type the dir command with the redirect symbol to generate the list of files and export it as a text file, the file is stored in C:\list_of_files.txt
dir > "C:\list_of_files.txt"
To batch list all files in directory and subdirectories,, repeat the last step with the recursively option:
dir /s > "C:\recursive_list_of_files.txt"
2. List all Windows files recursively in folders and subfolders
To list all the files recursively in all folders and also including subfolders, use a variation of the command. This variation explicitly displays the content of a folder using the full path. By default, the command would display various information such as the date and time of modification. And also the type, whether it is a directory or a file, and the size.
dir /s C:\Folder\SubFolder
3. Generate a text list of the files within a folder
Use the > and the folder name or full absolute path to write the list of the files directly into a result text file. Indeed, use the “>” superior symbol to redirect the output to a specific text file instead of displaying it to the command line screen. Simply add this redirection symbol at the end of the command.
dir C:\Folder\SubFolder > C:\Folder\list_of_files.txt
4. Display only the file names without metadata
By using the /b option in the CMD command, you can retrieve only the file names and extensions without any additional metadata. This can be useful if you only need a quick overview of the files in the folder, without any extra information such as the file size or date modified. When you add the /b option to the dir command, it will only display the file names and extensions in the output file, making it easier to view and analyze the list of files.
dir C:\Folder\SubFolder \b > C:\Folder\list_of_files.txt
5. Display files and exclude folders from the list
Another version of the script to make sure to display only the files and exclude the folders:
dir C:\Folder\SubFolder /a /-d /b > C:\Folder\list_of_files.txt
The dir /a option allows to specify the elements to display. And also the ones to exclude. The /-d option indicates not to display the folders. For different options of the dir command with no redirection used but simple displays in the command prompt screen, check different ways to list files in a folder with cmd.
Conclusion on exporting text list of files using Windows
To conclude this tutorial, cmd scripting is the best way to create a text list of Windows files stored in a folder. Indeed, by using the redirection operator “>”, the output can be saved in a text file. Hence making it easy to analyze, search, and manipulate the information as needed. This is especially helpful when dealing with a large number of files or complex folder structures.
With the right combination of MS-DOS commands and options, it is possible to customize the output. And for example to display only the information needed, such as file names or file paths. Also, quickly generating a list of files into a text file can save a lot of time. And also improve productivity for file management tasks.
I personally use it quite often and systematically when it comes to more than dozens of files. To go further, try copying recursively multiples files with the same name in different folders, and use the list of files to check duplicates.