Merge files

Posted on 2006-05-03
Last Modified: 2007-12-19
Hi Everyone,

I want to add to add 2 files in 1. But sometimes this first file won't have a carriage return/line feed an this wont merge the files properly. Here's an example.

File 1:
2222(no carriage return)

File 2:

With a copy 1 + 2 result, I'll have something like:

And I just want 1 row per line, This because the file "1" sometimes doesn't have the carriage return, do you know a command or a way to check if the carriage return ir missing?

Question by:amphill
    LVL 11

    Expert Comment

    you may try this (if you have no problems to have empty rows in your result file sometimes)
    create file cr.txt in which you put just one carriage return/line feed
    and create a bat file with these commands

    type 1 > 3
    type cr.txt > 3
    type 2 >> 3

    this will create file 3 with the content of 1
    add the CR/LF symbols from cr.txt to 3
    after that add the content of 2 to 3
    LVL 11

    Expert Comment

    this row (type cr.txt > 3) has to be
    type cr.txt >> 3
    LVL 30

    Accepted Solution

    You could do the following:

    @echo off


    REM ** Input files
    set file1=temp1.txt
    set file2=temp2.txt

    REM ** Output file
    set file3=temp3.txt

    if exist "%file3%" del "%file3%"

    for %%a in ("%file1%", "%file2%") do (
      for /f "delims=" %%b in ('type "%%a" 2^>NUL') do (echo %%b)>>"%file3%"

    echo Output in %file3%

    Feel free to change file1, file2, and file3.

    Good Luck,

    Author Comment

    Thsnk for the answer WelkinMaze, bu in this case there will be no emtpy rows, anyway, it's a good idea.

    Author Comment

    SteveGTR ... wow... that was fast and precise.
    That's exactly what I needed, this is the first time that I use batch files (ms-dos basis) and I will try to comprehend your batch.

    Thanks for this great answer, accepted.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (…
    VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now