Batch Script Writing to Files
Writing to Files
In Batch Script, 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.
note
Learn more about redirection in our Redirection Operators Chapter
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.
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