Displays subfolders as a comma seperated list

Don
Joined
21 Oct 2002
Posts
46,829
Location
Parts Unknown
Sure I've done this before, but can't think right now.


I've got a folder like this..

Folder\
Sub1
Sub2
Sub3
Sub4
file.txt



If I'm in Folder, what command can I use to create a file that will display them like this.

I assume along the lines of..

dir /b >folders.txt, but that lists then in a vertical line

folders.txt
Sub1,Sub2,Sub3,Sub4
 
Code:
for /f "delims=" %%a in ('dir /b /a:d') do <nul set/p "=%%a," >> folder.txt

Any good?

Does leave a trailing , at the end though and you'd need to delete the file first each time it runs or it'd just keep appending...

edit: or vbs

Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(".")
Set fc = f.SubFolders
For Each f1 in fc
	s = s & f1.name 
	s = s & ","
Next

Set t = fs.OpenTextFile("folder.txt", 2, True, 0)
t.WriteLine Left(s,Len(s)-1)
t.Close
 
Last edited:
Back
Top Bottom