2020-09-13

Creating a List of Files of a Directory With Command-line

I recently had to provide a list of files in a USB thumbdrive/stick and learned a sweet trick via CLI. This method provides a text, Word, or Excel file that lists all the files and folders inside a specific directory within your computer.

Basic command structure

Syntax:

DIR [pathname(s)] [display_format] [file_attributes] [sorted] [time] [options]

Example:

dir /s > output.doc

Flags explained:

  • dir - Lists all files and folders contained in the folder
  • /s - Lists all files in the subfolders as well
  • output.doc - The output file containing the directory listing (can also be .txt for editing in Notepad)

Listing only certain types of files

To filter specific file types, use wildcards:

dir /s *.pdf > output_pdf.doc

What this does:

  • *.pdf - Wildcard function that selects only .pdf files

List bare format (no heading, sizes or summary)

The /b switch lists file names only. When displaying subfolders with dir /b /s, the command will return a full pathname.

dir /b /s > output_pdf.doc

Using the tree command instead of dir

This command produces a tree listing of the current directory:

tree /f > output.txt

Flags explained:

  • /f - Displays the names of files within each directory listed
  • /a - Uses alternative (ASCII) characters to draw the tree diagram, useful for printers that don’t support line and box drawing characters