Command to list all files in a folder as well as sub-folders in windows

1,290,638

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.

enter image description here

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
Share:
1,290,638

Related videos on Youtube

user1760178
Author by

user1760178

Updated on July 08, 2022

Comments

  • user1760178
    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
      Admin over 11 years
      The below post gives the solution for your scenario. [SubDirectory Files Listing command][1] [1]: stackoverflow.com/questions/3447503/…
    • Carey Gregory
      Carey Gregory over 11 years
      dir /s does the job.
    • TaW
      TaW over 2 years
      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..
  • Jason
    Jason about 9 years
    This solution worked great with the added bonus of exporting the list to a .txt file.
  • Jimmy Adaro
    Jimmy Adaro over 7 years
    Works fine inside of Windows 10 installing window!
  • Loufs
    Loufs over 7 years
    Voted up, cause planet earth is amazing
  • Gusdor
    Gusdor about 7 years
    A description of the switches used would greatly improve this answer.
  • Ajith
    Ajith about 7 years
    How to print this to file? I tried >f.txt but not print exact i see
  • Rocket Spaceman
    Rocket Spaceman almost 7 years
    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.
  • m-smith
    m-smith over 6 years
    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.
  • tno2007
    tno2007 over 6 years
    Wow, great solution. You literally saved me 25 minutes... to create folders and copy files manually
  • Vyren
    Vyren over 6 years
    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.
  • RenniePet
    RenniePet over 6 years
    I 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
    RenniePet over 6 years
    To answer my own comment, you can use WinDirStat. See here: stackoverflow.com/a/37169035/253938
  • Bryan Rayner
    Bryan Rayner almost 6 years
    For PowerShell, try dir -s instead of the /s format for flags.
  • Muath
    Muath almost 6 years
    great answer >>>
  • SubJunk
    SubJunk almost 5 years
    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
  • MGB.py
    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
    Christian Stengel over 4 years
    use tree /a /f > output.doc .. to generate the tree as a file
  • Zameer Ansari
    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
    sj_959 over 2 years
    Found this way to get the relative path (it uses powershell, though) powershell.exe "Get-ChildItem -Recurse . | Resolve-Path -Relative" Courtesy
  • Radu Ursache
    Radu Ursache over 2 years
    this is the way