List files and paths in a directory tree (including files in subdirectories)


Recently I needed to list all of the files in a large directory tree. Each line needed to include the full path to the file, and I didn't want to include the directories themselves in the list, just the files. Here's how I did it:

dir /a:-d /s /b > files.txt

The dir command, as you probably know, lists files in the current directory, but the default settings are not useful here - it does not include file path, for one thing.

The /a: switch is for attributes. It can be used to list files based on their attributes. The -d option following /a: tells the dir command to exclude directories.

The /s switch is for subdirectories. This tells dir to loop through each subdirectory, listing all of the files in each. The geeky word for this is recursion, which means "recursion". :)

Note: if you use this switch on a directory with a lot of subdirectories and files, be prepared to wait a while. It may take several minutes for the command to run, and you won't see any feedback on the screen if you run the command as I did -- see below.

The last switch, /b means dir will use the "bare" listing format. That will give just the filepath and filename that I need.

What about the "> files.txt" on the end? This sends the output of the entire command to "files.txt", which will be created if it doesn't exist. Since I was listing over 41,000 files, it was much more useful to have the list of files in a plain text file instead of on the screen.


you can also change the default sorting options for the DIR command

Using the /o, This woudl be useful for sorting by size in your case


N  By name (alphabetic)

S  By size (smallest first)

E  By extension (alphabetic)

D  By date/time (oldest first)

G  Group directories first

-  Prefix to reverse order

dir /a:-d /s /b /o-s > files.txt

will also sort you output by size .


You can also make your change perminant by setting an environment vairable called DIRCMD

DIRCMD are the default switches appended to your dir command.

Mine is set dircmd=/ogen which groups by directory then extn then name

Good to know! Thanks Terry.