Batch Script - Writing to Files


Content writing to files is also done with the help of the redirection filter >. This filter can be used to redirect any output to a file. Following is a simple example of how to create a file using the redirection command to write data to files.


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

The above code snippet first uses the DIR command to get the directory listing of the entire C:\ . It then takes that output and with the help of the redirection command sends it to the file new.txt.


If you open up the file new.txt on your C drive, you will get the contents of your C drive in this file. Following is a sample output.

Volume in drive C is Windows8_OS
Volume Serial Number is E41C-6F43

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
12/17/2015 12:19 AM   <DIR>       04 - Software
12/15/2015 11:06 PM   <DIR>       05 - Studies
12/20/2014 09:09 AM   <DIR>       06 - Future
12/20/2014 09:07 AM   <DIR>       07 - Fitness
09/19/2015 09:56 AM   <DIR>       08 - Tracking
10/19/2015 10:28 PM   <DIR>       09 – Misc