3

I know I can use copy *.txt newfilename.txt to merge all the text files in a folder. Is there a way to do this, but separate each merged file with a line break?

2
  • What do you mean by a line break?
    – DavidPostill
    Apr 2, 2015 at 20:48
  • Say I have 2 files: one file has 3 lines: cat1 is on the first line. cat2 is on the second line and cat3 is on the third line. the second file has 1 line. hat1. how do i get it so that the merged file has hat1 on the fourth line, instead of cat3hat1 on the third line.
    – Newb
    Apr 2, 2015 at 21:01

2 Answers 2

6

CMD has several commands you can use to accomplish this.

  1. Type will show the contents of a file
  2. >>textfile.txt will append the output to an existig file
  3. echo. will print an empty line
  4. FOR will allow you to execute one or more commands based on a set of criteria, such as all files in a directory

Using these 4 commands, you can construct a way to accomplish what you want.

If you do not want to automate this as it are only a few files and you are doing it once, forget about the FOR and do it manually. (much easier)

The command would be this:

type file1.txt >> newfile.txt
echo. >> newfile.txt
type file2.txt >> newfile.txt
echo. >> newfile.txt
type file3.txt >> newfile.txt
echo. >> newfile.txt

If you need to include a FOR the command would be this:

FOR %f IN (*.txt) DO type %f >> newfile.log & echo. >> newfile.log

Do note: if the newfile.txt already exists, it will only append to it, not create a new file. Delete the old first

EDIT: corrected for and changed name. tnx Techie007 for pointing that out.

1
  • 3
    Except it will include newfile.txt in the For loop. :) Output to a different folder, or with a different extension. Also, as-is this command doesn't work due to the /F option: The system cannot find the file *.txt.. Apr 2, 2015 at 21:47
0

You can use the following command in dos

more file1.txt >> file2.txt
a blank line will get inserted after every file that is being merged,
this is useful when you have to merge a common file frequently.

further examples

more file1.dat >> output.dat

any extension with files that are of ascii characters can be merged and the original file will still be available unless you delete.

You can use the following command to merge and delete the second file.
copy file1.txt 00000.txt
more 00000.txt >> 11111.txt
del 00000.txt

you can merge any type of file extensions,
a blank line gets inserted after each file that is merged
commonly used extensions are .txt, .dat, .log, etc.,

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .