Merge Multiple file using CMD

-3

0

I have got plenty of *.cpp files in my directory. I have successfully merged file using the below command which i got from one of the sites online.

for %f in (*.cpp) do type "%f" >> Merged.doc 

Now i have got a file (Merged.doc) with the contents of all my .cpp files. I want to add blank page at the end of each .cpp while merging or I would like to have each .cpp files in different pages in the Merged file .

As the below command (from another question). Is there any command similar to :

type *.cpp > merged.doc

Here each file starts after a newline like this it should start in a new page.

EDIT

What if i need to add a four lines of text before each program . Suppose if i want to add the Date associated with each file in the line Date : DD-MM-YYYY what should i do and also what should i do to add the file name with File Name : abcd.cpp(I don't want his .cpp).

I'm new to this commands so please excuse me. I don't know why i have got a -ve vote for this question.

What i did :

 @echo off
 cd C:\programs
 echo ^<html^>^<head^>^<style^> pre {page-break-after: always;} ^</style^>^</head^>^<body^>      >merged.html
 for /r %%f in (*.cpp) do (
 echo ^<pre^>
 echo File Name        :(Here i want the date associated with each file without the extension .cpp) 
 echo File Description :
 echo Author           :Name
 echo Date             :(Here the date associated with each program)
 echo.
 type "%%f"
 echo ^</pre^>
 ) >>merged.html
 echo ^</body^>^</html^> >> merged.html

ERROR in the resultant file

After #include there is not or or or . But with my original program file it is all available.I does not have he whole program only the half part of each program is on the resultant file.What is the problem with the batch file?

CodeIt

Posted 2015-03-07T11:13:51.123

Reputation: 1 485

This is really lame. The answer is that text file no longer support page breaks because printers no longer support /b escape codes. If you want to have real working page breaks then you'll need to convert this to something else. I would suggest html if you want to print this. If you want page breaks for formatting alone then it cannot be done because almost no text editors support that escape code anymore (which once again is because it is useless when printing anyway). – krowe – 2015-03-07T15:19:15.487

You could count lines but that'll only work for a specific font and font size. You haven't given us that and even if you did it would be a pain to figure out for you. If you wanted to do this then just go here: http://stackoverflow.com/questions/5664761/how-to-count-no-of-lines-in-text-file-and-store-the-value-into-a-variable-using

– krowe – 2015-03-07T15:21:07.157

I'm ready to convert my file to any type but after everything i should be able to convert it into .doc or .rtf file . – CodeIt – 2015-03-07T15:21:21.260

@manutd Please don't keep changing your question to add new requirements after you have already have an answer. If you have another question, please ask it by clicking the Ask Question button.

– DavidPostill – 2015-03-15T14:16:55.133

Answers

0

This should do the trick:

@echo off
break>merged.cpp
for /r %%f in (*.cpp) do (
    type "%%f"
    for /l %%x in (1, 1, 100) do echo.
) >> merged.cpp

This is written to be ran from a batch file. If you're typing his out each time then you're wasting your time. Change the 100 to any number of returns you'd like.

Update

If HTML is fine then use this:

@echo off
cd C:\test
echo ^<html^>^<head^>^<style^> pre {page-break-after: always;} ^</style^>^</head^>^<body^> >merged.html
for /r %%f in (*.txt) do (
    echo ^<pre^>
    echo See, this is easy!
    echo File Name: %%f
    echo Author: The Dude
    echo.
    type "%%f"
    echo ^</pre^>
) >> merged.html
echo ^</body^>^</html^> >> merged.html

Update 2

If you want to get this into Word correctly (without buying the pro version of Acrobat) then you just want to print the document to a pdf file. Then open that pdf file in Word (just like you would open a *.doc file). Word will covert it correctly only if done exactly like I've explained (you should see a message box asking you weather you want to continue with the conversion after you click open).

Oh and your question about adding 4 lines of text before each source file shows that you've learned nothing from what I've shown you. Look at the first part of my answer again and take a good guess as to how that would be done.

krowe

Posted 2015-03-07T11:13:51.123

Reputation: 5 031

Can we have exactly one page between each programs.That is if a program ends in middle or at any position of a page the next file start after a blank page.I have edited my question please check . – CodeIt – 2015-03-07T15:08:45.977

@manutd See my update. – krowe – 2015-03-07T15:26:32.097

It didn't work . It just created a html file but no new page . – CodeIt – 2015-03-07T15:39:08.713

@manutd FYI, it is customary for the OP to not only accept an answer but to also upvote all answers which make a good attempt. Also, it did work. Just go to look at it in the print preview dialog. It'll print just as you asked. – krowe – 2015-03-07T15:39:53.357

It worked . Now i need to convert this file into .doc will i get each program in new page. – CodeIt – 2015-03-07T15:43:57.540

@manutd See my update. Then upvote me. – krowe – 2015-03-07T16:11:44.077

Before seeing your answer i have already created the PDF using the same method. Should i replace type "%%f" with the text i required to enter . – CodeIt – 2015-03-07T16:28:12.887

No. Just echo the text that you want (do that before the TYPE line). Use something like this: echo See, this is easy! – krowe – 2015-03-07T16:34:56.667

I have got several problems now after creating merged file . see my updated question . – CodeIt – 2015-03-14T04:06:22.647

@manutd Try something like this answer suggests: http://stackoverflow.com/a/10945887/932549 for the final little bit of this. Also don't forget to up vote all questions which helped you.

– krowe – 2015-03-16T02:59:26.977