37

How can I append file1 to file2, from a batch file? Text files and only using what is "standard" on windows.

1
  • 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. Apr 23, 2018 at 9:55

3 Answers 3

75

It is as simple as

type file1 >> file2
3
  • 4
    the type command does echo all contents of file1 and >> will append it to file2
    – Top-Master
    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 F
    Sep 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 Toal
    Sep 5, 2020 at 5:14
11

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.

2
  • 4
    if the last line of file1 does not end with a line feed (that's quite common with textfiles), the first line of file2 will be added to the last line of 'file1`
    – Stephan
    Feb 12, 2015 at 21:10
  • This result also has file1 and file2 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
6
COPY file1.txt+file2.txt result.txt
2
  • 1
    This result has file1 and file2reversed 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.
    – djna
    Sep 28, 2021 at 5:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.