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

7

292

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.

Skulk answered 5/3, 2013 at 1:55 Comment(3)
The below post gives the solution for your scenario. [SubDirectory Files Listing command][1] [1]: #3448003Burble
dir /s does the job.Airframe
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..Pectase
B
437

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.

Burble answered 5/3, 2013 at 2:13 Comment(5)
A description of the switches used would greatly improve this answer.Michellemichels
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.Statesmanship
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.Erlandson
For PowerShell, try dir -s instead of the /s format for flags.Inclusive
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.txtHypso
A
180

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.

Annalee answered 24/8, 2016 at 13:49 Comment(6)
Works fine inside of Windows 10 installing window!Kelwin
How to print this to file? I tried >f.txt but not print exact i seeEpigenesis
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?Glauconite
To answer my own comment, you can use WinDirStat. See here: https://mcmap.net/q/102153/-tree-view-of-a-directory-folder-in-windows-closedGlauconite
@Epigenesis 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 fileCommissar
use tree /a /f > output.doc .. to generate the tree as a fileAlgolagnia
C
84

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
Conjugal answered 15/5, 2015 at 9:39 Comment(4)
This solution worked great with the added bonus of exporting the list to a .txt file.Lanie
Wow, great solution. You literally saved me 25 minutes... to create folders and copy files manuallyFredella
great answer >>>Barela
Found this way to get the relative path (it uses powershell, though) powershell.exe "Get-ChildItem -Recurse . | Resolve-Path -Relative" CourtesyProtolithic
C
9

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
Cabral answered 21/6, 2017 at 12:52 Comment(2)
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.Olshausen
@Olshausen Thanks a lot for highlighting this! Can you please suggest an edit? I am more than happy for improvements :)Cabral
M
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.

Moonshine answered 9/8, 2017 at 12:56 Comment(0)
H
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

Hosea answered 24/10, 2022 at 13:23 Comment(0)
R
0

An addition to the above answers :

To print specific file present in the folders/sub-folders for eg : If you want to list just the csv files then :

dir /b/s/A-D/o:gn *.csv >list.txt

If you want to also include .xlsx files then the code is :

dir /b/s/A-D/o:gn *.csv *.xlsx >list.txt

You can mention more file types in the same way.

P.S Got to know this one from chatgpt :P

Reest answered 12/4 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.