How can I append file1 to file2, from a batch file? Text files and only using what is "standard" on windows.
3 Answers
It is as simple as
type file1 >> file2
-
4the
type
command doesecho
all contents offile1
and>>
will append it tofile2
Nov 28, 2018 at 5:51 -
Can be an issue using this with first line of second file on same line as last line of prev file. e.g. an SQL file ending in GO and an SQL file starting with GO I get GOGO on one line .. I will try appending a file containing just one empty line between all the other appends to get around this issue .. looks like Stephan commented same with answer below also.– Allan FSep 4, 2020 at 4:39
-
I'd argue that this is the correct behavior. A file is a sequence of bytes, whether or not the bytes are assumed to be encode text, or machine code, or images, or videos. Bytes are just bytes. Appending files is just appending two byte streams. If the
>>
operator were defined to automatically insert bytes representing a newline (and what encoding would you choose? which code points would you choose?) that would be wrong. Now if your intent is to automatically insert newlines at the ends of files that don't have them, you are correct, it is the programmer's responsibility!– Ray ToalSep 5, 2020 at 5:14
You might get better results with the following:
copy /b file1.txt+file2.txt result.txt
I use this command to combine dozens of XML files together. The "/b" puts the copy process in binary mode. This eliminated the control character that was appending to the end of my destination file.
-
4if the last line of
file1
does not end with a line feed (that's quite common with textfiles), the first line offile2
will be added to the last line of 'file1`– StephanFeb 12, 2015 at 21:10 -
This result also has
file1
andfile2
reversed from what the OP requested. Also, in case it matters to anyone, note that with this method (unlike when using>>
in another answer) the result file does not retain the same NTFS file identifier ("frn") as the file that was appended to, but rather gets a new file ID. Sep 27, 2021 at 22:48
COPY file1.txt+file2.txt result.txt
-
1This result has
file1
andfile2
reversed from what the OP requested. Sep 27, 2021 at 22:44 -
@GlennSlayden - so it is, 8 years and nobody else seems to have noticed! I wonder whether OP could figure it out.– djnaSep 28, 2021 at 5:27
copy *.txt output.dat
, I got to read across this today. Thought of posting here. This will copies and appends all the*.txt
file into single.dat
file.