res1
res1

Reputation: 3640

Appending data from a file to another file in a .bat file

I have a .bat file that generates a file from some previous commands, i need to add to this .bat file some dos commands that append the content of this file to another file, the names of the sourcefile and of the targetfile are fixed and both are text files.

There can be cases where the source file is not created from the commands on the .bat so maybe it is possible to add a check on this condition before executing the append command?

How can i do this?

I tried copy target+source target but sometimes using this i find the target file with some extra characters at line start, i don't know why.

Thanks

Upvotes: 1

Views: 6746

Answers (2)

j_random_hacker
j_random_hacker

Reputation: 51226

You cannot directly copy on top of the original file(s). Also make sure you use the /B switch for COPY to copy using "binary mode" -- failing to do this has the following negative consequences:

  1. Any "end-of-file" character (ASCII code 26) appearing in one of the source files will prematurely truncate the file at that point. Text files don't usually contain this character, but binary files (e.g. .EXE files or .DOC files) often do.
  2. An "end-of-file" character will be appended to the end of the output file.

Example of how to do it right:

copy /B input1 + input2 output
move /Y output input1

The move command moves the file output back on top of input1; /Y suppresses the "Overwrite?" prompt you would otherwise see.

Upvotes: 1

Bladean Mericle
Bladean Mericle

Reputation: 532

If your text was encoded UTF-8 or UTF-16, Maybe extra characters is BOM(Byte Order Mark)?
BOM exists start of file and has 3 byte length.
Is it match your probrem?

Upvotes: 0

Related Questions