Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite a text file from a batch file

Tags:

batch-file

dos

I am aware you can append to the end of a file with batch like so...

echo I like turtles >> file.txt 

but I would like to overwrite the file instead. How could this be done?

like image 721
James Avatar asked May 16 '13 14:05

James


People also ask

How do I overwrite a batch file?

This setting may be overridden with /-Y on the command line. The default is to prompt on overwrites unless COPY command is being executed from within a batch script. To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).

Can you edit a batch file while it running?

Short answer: yes, batch files can modify themselves whilst running.


2 Answers

Just answered my own question, this works:

echo I like turtles > file.txt                      ^                     |             Just one of these 
like image 156
James Avatar answered Sep 20 '22 23:09

James


You can use > instead of >> : this will overwrite the file

like image 37
Bathsheba Avatar answered Sep 20 '22 23:09

Bathsheba