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

7

72

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

Mats answered 10/8, 2010 at 9:17 Comment(0)
E
95

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
Emersed answered 24/11, 2011 at 17:21 Comment(3)
What if I only want folder names?Rolanda
Would you be so kind and update your answer how to get only files and not folders?Oriana
@HemusSan Only listing files would be this: dir /a:-d /s /b | sortActh
I
10

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)

Ingleside answered 14/12, 2012 at 0:58 Comment(1)
Not sure why the upvotes - this is clearly and testably WRONG. pastebin.com/qCUu4AHqEmersed
M
6

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

Macrogamete answered 5/4, 2012 at 15:26 Comment(0)
C
4

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

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

Chaille answered 29/11, 2016 at 7:1 Comment(0)
C
1

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

Chilpancingo answered 25/1, 2017 at 19:4 Comment(0)
M
1

Best way i found if you want to use Powershell:

Get-childitem -path "C:\your\path\here\" -recurse -name | out-file C:\your\path\to\textfile\here\files.txt

Then you will get the filenames inside the folder and subfolders written in a textfile.

Marcy answered 7/8, 2022 at 7:22 Comment(0)
M
0

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

Mcfarlin answered 17/7, 2013 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.