How to extract a list of folder files using CMD commands (feat. Powershell)
Sometimes you need to extract a list of files in a specific folder, but you don't want to do it manually because it takes too much time? In this post, we'll show you how to easily accomplish this task using CMD commands and the Powershell.
With this method, you can get the results you want with just a few simple commands. First, I'll show you how to extract the filenames of all the files in the folder you want into a CSV file, and then I'll walk you through the process of making some minor modifications after extraction (for example, extracting only 10 digits).
This guide will help you manage your files more efficiently and effectively, so let's get started!
How to open a CMD window
First, we'll open a Command Prompt (CMD) window, which you can do by following the steps below.
Run CMD from the Start menu
- Use hotkeys:
Windows key + Rto open the Run window. In the Run window, typecmdand enterEnteron the left side of the screen. - Use the Start menu: Click the Start button in the bottom-left corner of the screen, and in the search bar, type
cmdorCommand Promptin the search box. In the search results, click Command Prompt to run it.
Running CMD as administrator
- Use hotkeys:
Windows key + Xand then selectCommand Prompt (Admin)in the left sidebar. - Use the Start menu: In the search bar, type
cmdin the search results, and then clickCommand Promptand right-clickRun as administratorin the left sidebar. I recommend this option.
How to convert a list of all files in a folder to CSV
This article will show you step-by-step how to convert a list of all files in a folder to a CSV file using CMD and Powershell.
1. Save the list of files to a text file using the CMD command
First, open a CMD window and type (paste) the command below to convert all filenames within the specified folder (C:\test) to a text file (file_list.txt) in the folder. You can use the address of the folder of your choice instead of C:\test.
dir /b "C:\test" > file_list.txt2. convert a text file to a CSV file
Next, we'll use Powershell to read the text file and convert it to a CSV file.
powershell -Command "Get-Content file_list.txt | Out-File file_list.csv -Encoding ascii"3. Delete the temporary text file
A temporarily created text file (file_list.txt) to complete the operation.
del file_list.txtRunning an entire command at once
We've described it in steps to help you understand what's going on, but you can execute all of the above steps at once. Copy and paste the commands below into a CMD window and run the Enter key to run it.
cd "C:\test"
dir /b > file_list.txt
powershell -Command "Get-Content file_list.txt | Out-File file_list.csv -Encoding ascii"
del file_list.txtThis will make the file_list.csv The file will store the names of all the files in the folder, as shown below.
0292021001R00.pdf
Contents of the file_list.csv file
2192014006R01.pdf
2372019002R00.pdf
2442015001R01.pdf
But when I create the actual CSV file and check the contents, I see that the filename and extension are reflected. If you don't want to use it as it is, but just the filename, and maybe only a few digits of it, you can do the following. In this case, we're just going to extract the first 10 digits.
How to extract only the first 10 digits from a filename and save it as a CSV
First, we'll use the CMD command to extract the list of folder files and save it to a text file, just like we did before.
dir /b "C:\test" > file_list.txtUse Powershell to extract only the first 10 digits from each filename and save it to a CSV file. You can substitute any other number for 10 in the command below to extract any number of digits.
powershell -Command "Get-Content file_list.txt | ForEach-Object { $_.Substring(0,10) } | Out-File file_list.csv -Encoding ascii"It will also delete any text files you've already created.
del file_list.txtWe've written a CMD command to run all of the above steps at once. Copy and paste the command below into the CMD window and run the Enter key to run it.
cd "C:\test"
dir /b > file_list.txt
powershell -Command "Get-Content file_list.txt | ForEach-Object { $_.Substring(0,10) } | Out-File file_list.csv -Encoding ascii"
del file_list.txtThis will allow you to create a file_list.csv Only the first 10 digits of each filename are stored in the file.
0292021001
Contents of the file_list.csv file
2192014006
2372019002
2442015001
Organize
In this post, we learned how to list all the filenames in a folder of our choice with a single CMD command.
It doesn't matter what the command consists of, it matters that you know it and can make a list of it, right? In that sense, you've gained another skill.
Next time, we'll introduce another skill.
Thank you.
#Notes
Below is a detailed explanation of the CMD commands, in case you're interested in learning more.
Convert a list of all files in a folder to CSV
```cmd
cd "C:\test"
dir /b > file_list.txt
powershell -Command "Get-Content file_list.txt | Out-File file_list.csv -Encoding ascii"
del file_list.txt
```
These commands run in the following order
1. `cd "C:\test"`:
- Navigates to the "C:\test" directory.
- The `cd` command stands for "Change Directory" and is used to navigate to the specified path.
2. `dir /b > file_list.txt`:
- Gets a list of files within the current directory (C:\test) and saves it to the file `file_list.txt`.
- The `dir` command lists the files and folders within a directory.
- The `/b` option prints only filenames.
- The `>` saves the output to a file.
3. Run the following command: `powershell -Command "Get-Content file_list.txt | Out-File file_list.csv -Encoding ascii"`:
- Read the contents of the `file_list.txt` file and save it to the `file_list.csv` file.
- powershell -Command` runs a Powershell command.
- Get-Content file_list.txt` gets the contents of the `file_list.txt` file.
- A `|` (pipe) passes the output of the preceding command to the following command.
- Out-File file_list.csv -Encoding ascii` saves the contents to the file `file_list.csv`, specifying the encoding format as ASCII.
4. `del file_list.txt`:
- Deletes the temporarily created `file_list.txt` file.
- The `del` command deletes the specified file.
These commands are executed at once to save all the filenames in the `C:\test` directory to the `file_list.csv` file and delete the temporary files.
Extracting only the first 10 digits of a filename and saving it as a CSV
```cmd
cd "C:\test"
dir /b > file_list.txt
powershell -Command "Get-Content file_list.txt | ForEach-Object { $_.Substring(0,10) } | Out-File file_list.csv -Encoding ascii"
del file_list.txt
```
These commands are executed in the following order
1. `cd "C:\test"`:
- Navigate to the "C:\test" directory.
2. `dir /b > file_list.txt`:
- Gets a list of files in the current directory (C:\test) and saves it to the file `file_list.txt`.
3. Run the following command: `powershell -Command "Get-Content file_list.txt | ForEach-Object { $_.Substring(0,10) } | Out-File file_list.csv -Encoding ascii"`:
- Reads the contents of the `file_list.txt` file, extracts only the first 10 digits from each filename, and saves it to the `file_list.csv` file.
- powershell -Command` runs a Powershell command.
- Get-Content file_list.txt` gets the contents of the `file_list.txt` file.
- A `|` (pipe) passes the output of the preceding command to the following command.
- ForEach-Object { $_.Substring(0,10) }` extracts the first 10 digits from each filename.
- ForEach-Object` is a Powershell command that performs the specified action on each item.
- `{ $_.Substring(0,10) }` is a script block that extracts the first 10 digits of the filename.
- $_` is a Powershell automatic variable that represents the item currently being processed.
- Substring(0,10)` is a method that extracts the first 10 characters of a string.
- Out-File file_list.csv -Encoding ascii` saves the results to the `file_list.csv` file, specifying the encoding format as ASCII.
4. `del file_list.txt`:
- Deletes the temporarily created `file_list.txt` file.
These commands run together to extract the first 10 digits of all filenames in the `C:\test` directory, save them to the `file_list.csv` file, and delete the temporary file.





