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

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

This question is related to command-line directory dos

The answer is


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


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

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


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)


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


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


Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to directory

Moving all files from one directory to another using Python What is the reason for the error message "System cannot find the path specified"? Get folder name of the file in Python How to rename a directory/folder on GitHub website? Change directory in Node.js command prompt Get the directory from a file path in java (android) python: get directory two levels up How to add 'libs' folder in Android Studio? How to create a directory using Ansible Troubleshooting misplaced .git directory (nothing to commit)

Examples related to dos

How to increment variable under DOS? MS-DOS Batch file pause with enter key How to list files using dos commands? Commenting multiple lines in DOS batch file copy all files and folders from one drive to another drive using DOS (command prompt) In MS DOS copying several files to one file How to get a list of sub-folders and their files, ordered by folder-names How do I increment a DOS variable in a FOR /F loop? Recursive directory listing in DOS DOS: find a string, if found then run another script