How to get a list of sub-folders and their files, ordered by folder-names

Command LineDirectoryDos

Command Line Problem Overview


Can I use dir command-line to get a list of sub-folders and their files, ordered by folder-names, and not just file-names ?

using

> dir /s/b/o:gn > f.txt

I first get all sub-folders and only then all sub files, e.g.:

 d:\root0\root1\folderA
 d:\root0\root1\folderB
 d:\root0\root1\file00.txt
 d:\root0\root1\file01.txt
 d:\root0\root1\folderA\fileA00.txt
 d:\root0\root1\folderA\fileA01.txt
 d:\root0\root1\folderB\fileB00.txt
 d:\root0\root1\folderB\fileB01.txt

But I want to get -

d:\root0\root1\file00.txt
d:\root0\root1\file01.txt
d:\root0\root1\folderA
d:\root0\root1\folderA\fileA00.txt
d:\root0\root1\folderA\fileA01.txt
d:\root0\root1\folderB
d:\root0\root1\folderB\fileB00.txt
d:\root0\root1\folderB\fileB01.txt

["file00.txt" and "file01.txt" can also be at the end of the list]

Thanks,

Atara

Command Line Solutions


Solution 1 - Command Line

How about using sort?

dir /b /s | sort

Here's an example I tested with:


dir /s /b /o:gn

d:\root0
d:\root0\root1
d:\root0\root1\folderA
d:\root0\root1\folderB
d:\root0\root1\file00.txt
d:\root0\root1\file01.txt
d:\root0\root1\folderA\fileA00.txt
d:\root0\root1\folderA\fileA01.txt
d:\root0\root1\folderB\fileB00.txt
d:\root0\root1\folderB\fileB01.txt

dir /s /b | sort

d:\root0
d:\root0\root1
d:\root0\root1\file00.txt
d:\root0\root1\file01.txt
d:\root0\root1\folderA
d:\root0\root1\folderA\fileA00.txt
d:\root0\root1\folderA\fileA01.txt
d:\root0\root1\folderB
d:\root0\root1\folderB\fileB00.txt
d:\root0\root1\folderB\fileB01.txt

To just get directories, use the /A:D parameter:

dir /a:d /s /b | sort

Solution 2 - Command Line

Hej man, why are you using this ?

> dir /s/b/o:gn > f.txt (wrong one)

Don't you know what is that 'g' in '/o' ??

Check this out: http://www.computerhope.com/dirhlp.htm or dir /? for dir help

You should be using this instead:

> dir /s/b/o:n > f.txt (right one)

Solution 3 - Command Line

dir /b /a-d /s *.* will fulfill your requirement.

Solution 4 - Command Line

Command to put list of all files and folders into a text file is as below:

Eg: dir /b /s | sort > ListOfFilesFolders.txt

Solution 5 - Command Line

In command prompt go to the main directory you want the list for ... and type the command tree /f

Solution 6 - Command Line

create a vbs file and copy all code below. Change directory location to wherever you want.

Dim fso
Dim ObjOutFile

Set fso = CreateObject("Scripting.FileSystemObject")

Set ObjOutFile = fso.CreateTextFile("OutputFiles.csv")

ObjOutFile.WriteLine("Type,File Name,File Path")

GetFiles("YOUR LOCATION")

ObjOutFile.Close

WScript.Echo("Completed")

Function GetFiles(FolderName)
    On Error Resume Next
 
    Dim ObjFolder
    Dim ObjSubFolders
    Dim ObjSubFolder
    Dim ObjFiles
    Dim ObjFile

    Set ObjFolder = fso.GetFolder(FolderName)
    Set ObjFiles = ObjFolder.Files
 
    For Each ObjFile In ObjFiles
    ObjOutFile.WriteLine("File," & ObjFile.Name & "," & ObjFile.Path)
    Next
  
    Set ObjSubFolders = ObjFolder.SubFolders
 
    For Each ObjFolder In ObjSubFolders
    
        ObjOutFile.WriteLine("Folder," & ObjFolder.Name & "," & ObjFolder.Path)
     
    
        GetFiles(ObjFolder.Path)
    Next
 
End Function

Save the code as vbs and run it. you will get a list in that directory

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAtaraView Question on Stackoverflow
Solution 1 - Command LineCylindricView Answer on Stackoverflow
Solution 2 - Command LineappsomobileView Answer on Stackoverflow
Solution 3 - Command LineTamal GhoshView Answer on Stackoverflow
Solution 4 - Command LineAmar HRView Answer on Stackoverflow
Solution 5 - Command LinesamamedhView Answer on Stackoverflow
Solution 6 - Command LineTeemoView Answer on Stackoverflow