Print list of files and folders in Windows

Export list of all folders and sub-folders:

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

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

dir M:\ /s /on /b > C:\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 as 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 a s 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.


Sources:

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/dir