MS DOS directory: list to file

MS DOS directory: list to file

Sometimes it can be useful to list the contents of one or more directories in a text file, for example by creating a file catalog. Windows does not allow you to perform this operation from File Explorer, but it can be done from the command prompt, using MS-DOS commands such as the DIR command, which is responsible for listing the contents of a directory and displaying it on the screen.

How to list contents of a directory in a file?

In MS-DOS (or in Windows from the command prompt) it is possible to send the information obtained by the DIR command to a text file instead of displaying it on the screen. This can be achieved by using the > address sign. For example:

  • dir c:\mydir>myfile.txt

If you want to include the contents of more directories in the same file, just execute the DIR command and for each subsequent directory, use the >> sign. This symbol has the same function as the > sign, but does not delete the file contents, but rather adds the result obtained by the command.

  • dir c:\mydir>>myfile.txt

The previous method lists the files in a directory with all of their details, but has the disadvantage of adding two lines of presentation to the directory at the beginning of the result and two lines with the sum of the total size of the files and their number at the end. However, the following command gives a list of the files with their full path and an automatic path to the subdirectories and is more compact:

  • dir /b/s c:\mydir>myfile.txt

How addressing occurs in MS-DOS?

As you may have noticed, the DIR command shows the contents of the directory on the screen, while the > sign in simple terms means: everything to the left of the sign will be shown/saved in that which is to the right of the sign.

When you use this command with the DIR sign, nothing will appear on the screen, since the idea of the command is to save the result in a text file instead of displaying it. You can use this with any DOS command. For example, MEM > myfile.txt will save the current system memory information to the file myfile.txt.

It's even possible to direct the standard output of the DIR command and send the result directly to a printer. For example, MEM > myfile.txt will save the current system memory information in the file myfile.txt:

  • dir > lpt1

It will send the contents of the current directory to the printer, which will print it (if connected to the lpt1 port...). 

Any more Microsoft questions? Check out our forum!