Tuesday, May 14, 2013

Insert a Line of Text or Record to the Beginning of a Text File Using Windows Batch Script


I would like to share a Windows batch script that would add a line of text / record to the beginning of a text file.

Text File: example.txt
Text to be inserted: Hello World!

Script:
COPY example.txt temp.txt
ECHO Hello World!>example.txt
TYPE temp.txt >>example.txt
DEL temp.txt

Explanation:
1) Copy the example.txt content to a temporay file
COPY example.txt temp.txt

2) Overwrite the example.txt content with new text we need to add to the beginning
ECHO Hello World!>example.txt


3) Append  the content of temp.txt to example.txt
TYPE temp.txt >>example.txt

4) Delete the temp.txt file
DEL temp.txt

It worked well! Share your comments for queries, improvements and suggestions.