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.

- 1,048,767
- 296
- 4,058
- 3,343

- 6,277
- 5
- 27
- 57
-
1The 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
-
1If 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 Answers
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.

- 160
- 2
- 14
-
8
-
1This 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
-
1This 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
-
4For PowerShell, try `dir -s` instead of the `/s` format for flags. – Bryan Rayner Jul 31 '18 at 15:23
-
6Great 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
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.
Answering late. Hope it help someone.

- 55,015
- 38
- 216
- 226
-
1
-
1
-
1I know the OP asked for a command, but I'm wondering if you know of a GUI-style way of getting the same tree-like display of directories and files? – RenniePet Jan 09 '18 at 03:26
-
1To answer my own comment, you can use WinDirStat. See here: https://stackoverflow.com/a/37169035/253938 – RenniePet Jan 22 '18 at 02:53
-
@Ajith you need to write the command properly. Example: cd C:\Users\username\Desktop tree /F #Shows the tree only tree /F>C:\Users\username\Desktop\tree.doc #Saves the tree to tree.doc file – MGB.py Dec 13 '19 at 09:48
-
5use tree /a /f > output.doc .. to generate the tree as a file – Christian Stengel Mar 05 '20 at 10:11
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

- 5,080
- 8
- 48
- 76

- 3,669
- 1
- 21
- 17
-
2This 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
-
-
3Found 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
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

- 28,977
- 24
- 140
- 219
-
3Without 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
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.

- 5,772
- 1
- 26
- 43
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

- 227
- 3
- 4