Skip to content

Batch Script Writing to Files

Learn how to use the redirection operator > in Windows Batch Script to write command output (like dir) to files such as new-file.txt for logging or data extraction.

Example

The following code snippet first uses the dir command to get the directory list of the entire C:\. It then takes this output and, with the help of the redirect command, sends it to the file new-file.txt.

@echo off
dir C:\>C:\new-file.txt

If you open up the file new-file.txt on your C drive, you will get the contents of your C drive in this file.

Following is a sample output.

Outputplaintext
Volume in drive C is Windows10_OS
Volume Serial Number is E12C-3F45

Directory of C:\

12/22/2015 09:02 PM <DIR> 01 - Music
06/14/2015 10:31 AM <DIR> 02 - Videos
09/12/2015 06:23 AM <DIR> 03 - Pictures
10/19/2015 10:28 PM <DIR> 04 – Misc
Navigation