274

I tried searching for a command that could list all the file in a directory as well as subfolders using a command prompt command. I have read the help for "dir" command but coudn't find what I was looking for. Please help me what command could get this.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1760178
  • 6,277
  • 5
  • 27
  • 57
  • 1
    The below post gives the solution for your scenario. [SubDirectory Files Listing command][1] [1]: http://stackoverflow.com/questions/3447503/how-to-get-a-list-of-sub-folders-and-their-files-ordered-by-folder-names –  Mar 05 '13 at 02:10
  • 14
    `dir /s` does the job. – Carey Gregory Mar 05 '13 at 02:11
  • 1
    If you are in europe you may want to do a `chcp 1252` before any of the below solutions to get our special characters right in windows.. – TaW Dec 07 '21 at 22:43

6 Answers6

421

The below post gives the solution for your scenario.

dir /s /b /o:gn

/S Displays files in specified directory and all subdirectories.

/B Uses bare format (no heading information or summary).

/O List by files in sorted order.

Then in :gn, g sorts by folders and then files, and n puts those files in alphabetical order.

Freerey
  • 160
  • 2
  • 14
  • 8
    A description of the switches used would greatly improve this answer. – Gusdor May 03 '17 at 11:34
  • 1
    This outputs the path + filename not just the filename. This doesn't work. When recursive /s is added, DIR will always output the full paths in outputs. So a FOR script would likely be needed to recursively find all filenames inside a directory tree and output them in alphabetical order in a text file. – Rocket Spaceman Jul 08 '17 at 01:14
  • 1
    This is a great option. However, it sadly doesn't seem to work in PowerShell, which means I seem to be unable to use this command on a UNC path. – m-smith Oct 12 '17 at 10:49
  • 4
    For PowerShell, try `dir -s` instead of the `/s` format for flags. – Bryan Rayner Jul 31 '18 at 15:23
  • 6
    Great answer. In addition to this, because of how difficult it is to do things like copy specific parts of text from a vanilla command prompt, it can be good to append `>list.txt` to make it output to a file to be more easily used. So the command would be: `dir /s /b /o:gn >list.txt` – SubJunk Jun 28 '19 at 02:55
166

If you want to list folders and files like graphical directory tree, you should use tree command.

tree /f

There are various options for display format or ordering.

Check example output.

enter image description here

Answering late. Hope it help someone.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
77

An addition to the answer: when you do not want to list the folders, only the files in the subfolders, use /A-D switch like this:

dir ..\myfolder /b /s /A-D /o:gn>list.txt
micstr
  • 5,080
  • 8
  • 48
  • 76
Laszlo Lugosi
  • 3,669
  • 1
  • 21
  • 17
  • 2
    This solution worked great with the added bonus of exporting the list to a .txt file. – Jason May 27 '15 at 23:09
  • Wow, great solution. You literally saved me 25 minutes... to create folders and copy files manually – tno2007 Nov 10 '17 at 16:48
  • great answer >>> – Muath Sep 25 '18 at 18:01
  • 3
    Found this way to get the relative path (it uses `powershell`, though) `powershell.exe "Get-ChildItem -Recurse . | Resolve-Path -Relative"` [Courtesy](https://superuser.com/questions/1029558/list-files-in-a-subdirectory-and-get-relative-paths-only-with-windows-command-li) – sj_959 Dec 21 '21 at 13:33
8

If you simply need to get the basic snapshot of the files + folders. Follow these baby steps:

  • Press Windows + R
  • Press Enter
  • Type cmd
  • Press Enter
  • Type dir -s
  • Press Enter
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • 3
    Without any arguments, `dir` only gives information about the files and directories in the current folder, but the OP wants the return to include files in subfolders as well. – Vyren Nov 26 '17 at 17:58
  • @Vyren Thanks a lot for highlighting this! Can you please [suggest an edit](https://stackoverflow.com/posts/44676699/edit)? I am more than happy for improvements :) – Zameer Ansari Apr 09 '20 at 09:45
7

An alternative to the above commands that is a little more bulletproof.

It can list all files irrespective of permissions or path length.

robocopy "C:\YourFolderPath" "C:\NULL" /E /L /NJH /NJS /FP /NS /NC /B /XJ

I have a slight issue with the use of C:\NULL which I have written about in my blog

https://theitronin.com/bulletproofdirectorylisting/

But nevertheless it's the most robust command I know.

Bruno
  • 5,772
  • 1
  • 26
  • 43
5

The below post gives the solution for your scenario.

**dir /s /b /o:gn**

/S Displays files in specified directories and all subdirectories.

/B Uses bare format (no heading information or summary).

/O List by files in sorted order.

:gn, g sorts by folders and then files, and n puts those files in alphabetical order.

Just for all files except long path, write the following command:

**dir /b /o:gn**

For Tree: write in your cmd

tree /f

Tabish Zaman
  • 227
  • 3
  • 4