Print list of files and folders in Windows

Export list of all folders and sub-folders only in a folder:

dir /s /on /b /a:d >output.txt

Export list of all folders and sub-folders including files and their file extension.

dir /s /on /b >output.txt

b lists all files in “bare format” (no heading information or summary). /s Lists all sub-directories and their files, not just the files in the selected folder. /on sorts the list by folder structure and filename. If you have for example Japanese or Chinese characters in the filenames they will just return ????? instead of the character. To fix this you can run the command chcp 65001 before the dir command to make CMD print the list as Unicode. So for example, in a batch file, you could run:

@echo off
chcp 65001
set driveletter=M
set date="%date:~-4,4%%date:~-7,2%%date:~-10,2%"
dir %driveletter%:\ /s /on /b > %driveletter%:\"%date% - M-DriveOutput.txt"

This will list all files and folders of the drive letter you enter as a variable (in this case M:) and print it out to the root folder of the same drive as a TXT file. It will also add the current date as a date stamp to the filename of the text file. A great way to backup metadata only 👍