Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch file copy contents of 1 file into another

I am trying to create a bat file to copy the contents of one file and append it to the end of another file.

Say I have a file named test.txt and I want to append the contents of test.txt to a file that is already created with the name of results.txt

How can this be done?

like image 717
Wesley Avatar asked Nov 09 '10 19:11

Wesley


People also ask

How do I copy the contents of a file to another file?

You need to use the cp command. It is used to copy files and directories. The copies become independent of the originals. Any subsequent change in one will not affect the other.

How do you copy the content of a file to another file in cmd?

If you want a content of a file in another file you can use type command to display the content and > to redirect the output to a file. You could just us copy command like @copy x. txt results. txt .

What does %% mean in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


2 Answers

You might want to use the "copy" command (you can use it within your batch file), it'll do ascii or binary mode, alloows for appending.

Copies one or more files to another location.

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

source Specifies the file or files to be copied. /A
Indicates an ASCII text file. /B Indicates a binary file. /D Allow the destination file to be created decrypted
destination Specifies the directory and/or filename for the new file(s). /V Verifies that new files are written correctly. /N Uses short filename, if available, when copying a file with a non-8dot3 name. /Y Suppresses prompting to confirm you want to overwrite an existing destination file. /-Y Causes prompting to confirm you want to overwrite an existing destination file. /Z Copies networked files in restartable mode.

The switch /Y may be preset in the COPYCMD environment variable. This may be overridden with /-Y on the command line. Default is to prompt on overwrites unless COPY command is being executed from within a batch script.

To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).

So, to append file1 to file2 and call it newfile the command would be

copy file1+file2 newfile

To just append file2 to file1 the command should be

copy file1+file2 file1
like image 38
Jim Avatar answered Sep 21 '22 13:09

Jim


You can do type test.txt >> results.txt

Also see this reference at ss64.com for redirection.

Note that TYPE will convert Unicode files to ANSI. If you need to keep files as they are, download the UnxUtils and use cat instead of type.

like image 148
Benoit Avatar answered Sep 23 '22 13:09

Benoit