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.