Skip to Content

Merge (combine) 2 text files

Merge to 2 text files into 1 new file all residing on the SAP server.

I couldn’t find a class with a method to merge 2 text files that are on the SAP server so I wrote a simple method myself. You can find the source code below. Does anyone know of an existing SAP class for handling files that are on the SAP server?

Parameters

Parameters

Source code

METHOD merge_files.
*——————————————————————–*
* PRE:  – max filename including path is 128
*       – only text files can be used
* POST: n/a
*——————————————————————–*
* declarations
  DATA:
    l_line      TYPE string,
    l_stop      TYPE boolean,
    tb_file01   TYPE ccmctltxt,
    tb_file02   TYPE ccmctltxt,
    tb_file_new TYPE ccmctltxt.

* open new file
  OPEN DATASET i_file_new FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

* load file 01
  OPEN DATASET i_file1 FOR INPUT IN TEXT MODE ENCODING DEFAULT.

* copy file1 to new data set
  WHILE l_stop <> ‘X’.
    READ DATASET i_file1 INTO l_line.
    IF sy-subrc <> 0.
      l_stop = ‘X’.
      EXIT.
    ENDIF.
    TRANSFER l_line TO i_file_new.
    CLEAR: l_line.
  ENDWHILE.

* close file1
  CLOSE DATASET i_file1.

* load file 02
  OPEN DATASET i_file2 FOR INPUT IN TEXT MODE ENCODING DEFAULT.

* copy file2 to new data set
  clear: l_stop.
  WHILE l_stop <> ‘X’.
    READ DATASET i_file2 INTO l_line.
    IF sy-subrc <> 0.
      l_stop = ‘X’.
      EXIT.
    ENDIF.
    TRANSFER l_line TO i_file_new.
    CLEAR: l_line.
  ENDWHILE.

* close files
  CLOSE DATASET i_file2.
  CLOSE DATASET i_file_new.

ENDMETHOD.

To report this post you need to login first.

12 Comments

You must be Logged on to comment or reply to a post.

  1. Mike Bennett
    Can anyone see the second picture?  This points to a C Drive path.

    Otherwise, very good, just the sort of thing I look for in the SDN.

    Thanks again!

    Mike Bennett
    Magnolia Consulting

    (0) 
    1. Dennis van den Broek Post author
      Hi Mike,

      Not sure what you mean, there is just the 1 picture that shows the methods parameters. Do you get an error om the page or …?

      Regards,
      Dennis

      (0) 
  2. Mike Bennett
    Dennis,

    there is a graphic between the picture under Parameters and just before Source Code that points to:

    file:///C:/DOCUME%7E1/DVANDE%7E1/LOCALS%7E1/Temp/moz-screenshot-1.jpg

    Thought you might like to know, my original comments still stand, keep it up!

    Mike Bennett
    Magnolia Consulting

    (0) 
    1. Dennis van den Broek Post author
      Hi Mike,

      Thanks for the additional info. I had to go into the HTML code to remove the entry. As I didn’t have the problem on my PC I cannot test the change myself, I hope it views alright now.

      Cheers,
      Dennis

      (0) 
  3. Andrea Galluccio
    Hi,
    why use classes ??

    If you are on NT machine you can create a system command (example merge.cmd) and in the file put a command link this :
    copy +%1 +%2 %3

    Then call the command from abap suing SXPG* function module using merge command with this parameter : c:\file1.txt c:\file2.txt c:\merge_file.txt

    You can do the same with binary file using the /B switc of the copy command.
    On the unix you can use the CAT command.

    Less abap… more quick 😉
    Andrea

    (0) 
    1. Dennis van den Broek Post author
      Indeed that is a good idea however the program must be able to run in the background. So in my case windows commands are out of the question.

      What I could have done is use a Unix (AIX flavour) command but this will make it OS dependend. The current solution is OS independent.

      I would still build a class & method to implement because this way you can have 1 class with several file handling methods. Also by using a method you can build-in error handling and you only have to do this once.

      Thanks for the tip.

      Cheers Dennis.

      (0) 
  4. Hi Dennis,

    what to do if you have to to merge 3 files?
    Or to append one or several texts to the existing file?

    What could you say about the code below?

    METHOD merge_files.
    *————————————————
    * PRE:  – max filename including path is 128
    *       – only text files can be used
    * POST: n/a
    *————————————————
    * two additional parameters instead of yours:
    *    i_appending TYPE flag
    *    it_files    LIKE TABLE OF i_file1
            ” or any table type, e.g. ccmctltxttab

    * declarations
      DATA:
        l_file2     LIKE i_file1,
        l_line      TYPE string.

    * open output file
      IF i_appending = ”.
        OPEN DATASET i_file1 FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
      ELSE.
        OPEN DATASET i_file1 FOR APPENDING IN TEXT MODE ENCODING DEFAULT.
      ENDIF.

      LOOP AT it_files INTO l_file2.
    *   load file2
        OPEN DATASET l_file2 FOR INPUT IN TEXT MODE ENCODING DEFAULT.

    *   copy file2 to output data set
        WHILE sy-subrc = 0.
          READ DATASET l_file2 INTO l_line.
          TRANSFER l_line TO i_file1.
          CLEAR: l_line.
        ENDWHILE.

    *   close file2
        CLOSE DATASET l_file2.
      ENDLOOP.

    * close output file
      CLOSE DATASET i_file1.

    ENDMETHOD.

    Are you agree that creating programs is more than just producing lines of code?

    Good luck,
    Vlad

    P.S. BTW, such a function exists – dx_merge_files…

    (0) 
    1. Dennis van den Broek Post author
      Hi Vald,

      Thanks for the tip. Does the dx_merge_files function work on files that are located on the SAP server? I didn’t have time to look at the function.

      Thanks also for the code suggestion it makes the method more useful at the time of writing there was no such requirement and you know how it goes there is always to much to do and to little time.

      Cheers Dennis

      (0) 
      1. Hi Dennis,

        yes, of course, this function works with the SAP application server files, otherwise I didn’t mention it.

        Truly speaking, I think that “there was no such requirements” to create such a strictly limited tool that merges only two texts into the new one and cannot append or process more files…

        You named your blog as the 1st of a series…
        Don’t hurry to publicate them, write and rewrite and revise and add useful options and find more optimal and universal solution in each case… Then and only then your blogs will be really helpful and popular…

        Good luck,
        Vlad

        (0) 

Leave a Reply