87

I am trying to print a list of the folders and sub folders of a directory to a file.

When I run dir /s/b/o:n > f.txt, I get a list of the files also. I only need the folders and sub folders.

Anyone know is this possible to do this from command line interface?

peterh
  • 11,875
  • 18
  • 85
  • 108
Inkey
  • 2,189
  • 9
  • 39
  • 64

6 Answers6

164

Try this:

dir /s /b /o:n /ad > f.txt
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Hi, this above command also lists the hidden and system folders. Can we ignore this (even if Windows Folders option has been to display such folders) – user187023 Nov 02 '14 at 02:52
  • thanks from me too! Do you stil think this will be supported in Windows 10 - I mean DOS commands? – Andrew Simpson Feb 08 '15 at 09:16
  • 9
    Not sure why you mix notation?, just use either `dir /s /b /o:n /a:d > f.txt` or `dir /s /b /on /ad > f.txt`. Personally I use the `:` approach for attributes of a switch as it's clearer what is going on. – user692942 Feb 24 '15 at 13:48
  • @Lankymart Very good point! Never haven given much attention about the different ways of the parameter notation. – Andreas Jun 30 '20 at 06:44
20
 Displays a list of files and subdirectories in a directory.

 DIR [ drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
  [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

  [drive:][path][filename]
          Specifies drive, directory, and/or files to list.

  /A          Displays files with specified attributes.
  attributes   D  Directories                R  Read-only files
           H  Hidden files               A  Files ready for archiving
           S  System files               I  Not content indexed files
           L  Reparse Points             -  Prefix meaning not

just set type of desired file attribute, in your case /A:D (directory)

dir /s/b/o:n/A:D > f.txt

evilruff
  • 3,947
  • 1
  • 15
  • 27
6

I don't have enough reputation to comment on any answer. In one of the comments, someone has asked how to ignore the hidden folders in the list. Below is how you can do this.

dir /b /AD-H
5

I used dir /s /b /o:n /a:d, and it worked perfectly, just make sure you let the file finish writing, or you'll have an incomplete list.

JosefZ
  • 28,460
  • 5
  • 44
  • 83
user4950040
  • 51
  • 1
  • 1
5

I am using this from PowerShell:

dir -directory -name -recurse > list_my_folders.txt
Sany
  • 103
  • 2
  • 8
3

dir /ad /b /s will give the required answer.

Superdooperhero
  • 7,584
  • 19
  • 83
  • 138
sravan
  • 31
  • 1