What is the easiest way to add a text to the beginning of another text file in Command Line (Windows)?
What is the 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:
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).
If you want to process bigger files the accepted solution becomes pretty slow. Then it's faster to use copy with a '+'
echo "my line" > newFile.txt
copy newFile.txt+myOriginalFile.txt combinedFile.txt
move /Y combinedFile.txt myOriginalFile.txt
del newFile.txt
Via PowerShell;
@("NEW Line 1","NEW Line 2") + (Get-Content "C:\Data\TestFile.txt") | Set-Content "C:\data\TestFile.txt"