Command to list all files in a folder as well as sub-folders in windows
Solution 1
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.
Solution 2
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.
Solution 3
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
Solution 4
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.
Solution 5
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
Related videos on Youtube
user1760178
Updated on July 08, 2022Comments
-
user1760178 almost 2 years
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.
-
Admin over 11 yearsThe below post gives the solution for your scenario. [SubDirectory Files Listing command][1] [1]: stackoverflow.com/questions/3447503/…
-
Carey Gregory over 11 years
dir /s
does the job. -
TaW over 2 yearsIf 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..
-
-
Jason about 9 yearsThis solution worked great with the added bonus of exporting the list to a .txt file.
-
Jimmy Adaro over 7 yearsWorks fine inside of Windows 10 installing window!
-
Loufs over 7 yearsVoted up, cause planet earth is amazing
-
Gusdor about 7 yearsA description of the switches used would greatly improve this answer.
-
Ajith about 7 yearsHow to print this to file? I tried >f.txt but not print exact i see
-
Rocket Spaceman almost 7 yearsThis 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.
-
m-smith over 6 yearsThis 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.
-
tno2007 over 6 yearsWow, great solution. You literally saved me 25 minutes... to create folders and copy files manually
-
Vyren over 6 yearsWithout 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. -
RenniePet over 6 yearsI 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 over 6 yearsTo answer my own comment, you can use WinDirStat. See here: stackoverflow.com/a/37169035/253938
-
Bryan Rayner almost 6 yearsFor PowerShell, try
dir -s
instead of the/s
format for flags. -
Muath almost 6 yearsgreat answer >>>
-
SubJunk almost 5 yearsGreat 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
-
MGB.py over 4 years@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
-
Christian Stengel over 4 yearsuse tree /a /f > output.doc .. to generate the tree as a file
-
Zameer Ansari about 4 years@Vyren Thanks a lot for highlighting this! Can you please suggest an edit? I am more than happy for improvements :)
-
sj_959 over 2 yearsFound this way to get the relative path (it uses
powershell
, though)powershell.exe "Get-ChildItem -Recurse . | Resolve-Path -Relative"
Courtesy -
Radu Ursache over 2 yearsthis is the way