Easiest way to add a text to the beginning of another text file in Command Line (Windows)

echo "my line" > newFile.txt
type myOriginalFile.txt >> newFile.txt
type newFile.txt > myOriginalFile.txt

Untested. Double >> means 'append'


Another variation on the theme.

(echo New Line 1) >file.txt.new
type file.txt >>file.txt.new
move /y file.txt.new file.txt

Advantages over other posted answers:

  • minimal number of steps
  • no temp file left over
  • parentheses prevents unwanted trailing space in first line
  • the move command "instantaneously" replaces the old version with the new
  • the original file remains unchanged until the last instant when it is replaced
  • the final content is only written once - potentially important if the file is huge.

The following sequence will do what you want, adding the line "new first line" to the file.txt file.

ren file.txt temp.txt
echo.new first line>file.txt
type temp.txt >>file.txt
del temp.txt

Note the structure of the echo. "echo." allows you to put spaces at the beginning of the line if necessary and abutting the ">" redirection character ensures there's no trailing spaces (unless you want them, of course).