What is the easiest way to add a text to the beginning of another text file in Command Line (Windows)?
7 Answers
echo "my line" > newFile.txt
type myOriginalFile.txt >> newFile.txt
type newFile.txt > myOriginalFile.txt
Untested. Double >> means 'append'

- 5,363
- 1
- 16
- 35

- 2,686
- 24
- 29
-
2It'll be "echo" instead of "type" for the first line but thanks, It worked. – dr. evil Feb 15 '09 at 11:34
-
Umm... no?..it's echo if it's a string like the example. Its type if you are reading a file... right? – Jakob Sternberg Sep 01 '18 at 00:58
-
@dr.evil what if I want to add text at a specific of line? for instance, a file having 100 lines and I want to add text at line no 20. – Ragesh Pikalmunde Feb 26 '20 at 05:45
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.

- 127,446
- 28
- 251
- 390
-
@AdrianBR Are you saying that the `type` could be put into the parentheses too, in order to avoid the 2 redirects to `file.txt.new`? I wondered about that but had to assume it's no because no one has posted it yet (and I can't test on Windows right now). And in that case, I wonder if we can then redirect the entire block including `type file.txt` back to the same `> file.txt`, avoiding the 2nd file and the `move` (but I guess not too, or someone would've said!) – underscore_d Mar 16 '18 at 18:39
-
2@underscore_d - Absolutely you can do that, though each command must be on a separate line, or you need `&` between the commands. It is actually faster to do that because file is only opened once for writing. My original code had the parentheses to prevent an unwanted space after the 1. Without parens and without space `echo New Line 1>file.txt.new` would interpret `1>` to mean redirect stdout. The 1 would not be written – dbenham Mar 16 '18 at 19:18
-
@dbenham Thanks, will try it out! I realise after what you said about the separate lines or `&` that I was unconsciously imagining this in the context of a batch file, whereas the OP asked about the command line, so that probably explains why no one suggested what I did, since it needs to be in a batch file to work (probably? I now realise I have no idea if `cmd` supports interactive blocks in brackets) – underscore_d Mar 16 '18 at 19:33
-
1@underscore_d - `cmd` does support "interactive blocks". The console continuously prompts for "More?" until you enter a line that closes the parentheses. – dbenham Sep 19 '18 at 09:48
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).

- 854,327
- 234
- 1,573
- 1,953
-
It's good, but in case of big files it may last very long - is there any more convenient way? – matandked May 09 '12 at 15:37
The following will also work:
echo "my line" > newFile.txt
type newfile.txt myOriginalFile.txt > myOriginalFile.txt
In the first line you are writing my line into newfile.txt. In the second line you are replacing the text from myOriginalFile.txt by overwriting it with the text from newfile.txt and myOriginalFile.txt, creating a new myOriginalFile.txt that contains both.

- 67
- 1
- 1
-
This does not work (Tested on Win 7). The original content gets lost, and the new line is repeated in the final content. – dbenham Nov 20 '13 at 22:18
-
This seems like a risky approach since, if anything goes wrong on the second line, you'll have corrupted myOriginalFile.txt – Stephen Lead Aug 18 '14 at 02:45
-
2
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

- 428
- 4
- 7
-
use of the /b option (binary) on the copy command avoids the SUB control character at the end of the resulting file – flyaround Aug 26 '22 at 15:12
If the first part of the line can be sorted, such as date/time then use the SORT /R command to put the most recent entries at the top of the file. The following will place a date/time stamp in the form of "YYYY-MM-DD HH:DD:SS AM/PM" to the start of each line:
echo %DATE:~-4%-%DATE:~7,2%-%DATE:~4,2% %TIME% "my text line" >> myOriginalFile.txt
sort /R myOriginalFile.txt /O myOriginalFile.txt
However, as files grow very large this method (as well as the ones above) become slow. Consider sorting only when required instead of with each entry - or use another scripting/programming language

- 21
- 1
Via PowerShell;
@("NEW Line 1","NEW Line 2") + (Get-Content "C:\Data\TestFile.txt") | Set-Content "C:\data\TestFile.txt"

- 1,231
- 13
- 25