How to generate a list of files in a directory with Command Prompt

Command prompt is available on almost every single version of Windows. It’s an oft-forgotten and undervalued tool for a huge range of automation processes, one of which I’ll be outlining today.


Very often in my line of work, I find myself in a situation when I need to be able to quickly export a list of files available in a directory so I can quickly analyse it (usually in Excel).

The quickest and simplest method I’ve found is to punch in a very simple code into the command prompt, and a text file containing my list magically appears in the directory! See below for a quick little tutorial on how to do it:


1. Open Command Prompt

First open up a file explorer window on your computer (specifically in the folder that you want the list of files from) and SHIFT+Right Click in any blank space around the files, you should see “Open command window here.”

If, instead of “Open command window here” you see “Open Powershell window here,” then your default command line editor is Powershell, which doesn’t use the same language as Command Prompt.  But don’t worry! You’ve probably still got Command Prompt on your computer. Just type “command prompt” into your Windows search bar. Upon opening it, you’ll need to change your directory before using the dir command.

2. Change Directory

If your Command Prompt is not looking at the right directory, you’ll need to point it to the directory you want your list of files from first. To change the directory your Command Prompt is pointing at, type in the following code:

cd /d and “file path”

eg. cd /d "C:\Users\JSmith\Documents"

Explanation:

  • cd returns the current directory
  • /d is a command to change the current directory

Tip: Enclose the file path in double quotes “” to avoid issues with ASCII characters like “&”

3. ‘dir’ Command

Once you’re in the right directory, you can print and export lists of filenames to your heart’s content with the dir command! To return a list of files in the directory and save to text file, type in the following code:

dir/b/o >fn.txt

Explanation:

  • dir returns a list of all the files names, file size & date/time modified
  • adding /b filters it to just the file names
  • adding /o sorts the list in alphabetical order
  • fn.txt exports as a text file and saves into the current directory.
  • the file doesn’t have to be called “fn.txt,” you can call it whatever you want, eg. “fn.txt” or “filename.txt” or “list of files.txt” or “dinosaurs_are_great.txt”

There are tonnes of arguments you can add to the dir command, check out a full list  here.


That’s it! I hope this tutorial has been helpful to you, I definitely make use of this little trick quite a lot. Thank you for reading, please let me know in the comments if there is any tutorials you would love to see!

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s