Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Append/Concatenate Files?

Former Member
0 Kudos

How can we concatenate two flat files?

I have two files and before using it as a parameter , I need to concatenate/append the contents of one file into another and then send it for processing ..!!

These are two flat files getting orders01 data in EDI850 format . I need to make them a single file and pass it to RSEINB00. ( as the files should be posted as a whole or fail if any error in any of them , thats why i cant call RESINB00 twice with these filename in my job )

1 ACCEPTED SOLUTION

nablan_umar
Active Contributor
0 Kudos

Hi Raymond,

This file looks like an Idoc file. So you may need to be careful when combining them since they contain segments that need to be in sequence. Anyhow, to concatenate 2 PC files, you can upload each using GUI_UPLOAD function module, which will store into an internal table. Then append the second table to the first one using APPEND command. Then download the file using function module GUI_DOWNLOAD.

7 REPLIES 7

former_member181966
Active Contributor
0 Kudos

I did it at OS level . Here’re the steps I followed.

-Create OS command in SM69 and give the UNIX path

-Test the OS command By tr.SM49

-Create Shell script

Cat filename >> newfile ( this Unix command add the content of the file into the new file . you can also use extension of the file like

CAt *.txt >> newfile ( If you’ll search on the UNIX forums( www.google.com ) , you’ll also find the code )

-Create SAP program with FM "SXPG_COMMAND_EXECUTE" and execute the command using SAP

Code for OS command

call function 'SXPG_COMMAND_EXECUTE'

exporting

commandname = c_os_command

additional_parameters = l_os_parameters

operatingsystem = sy-opsys

importing

status = l_status_code

exitcode = l_exit_code

tables

exec_protocol = log_messages

exceptions

no_permission = 1

command_not_found = 2

parameters_too_long = 3

security_risk = 4

wrong_check_call_interface = 5

program_start_error = 6

program_termination_error = 7

x_error = 8

parameter_expected = 9

too_many_parameters = 10

illegal_command = 11

wrong_asynchronous_parameters = 12

cant_enq_tbtco_entry = 13

jobcount_generation_error = 14

others = 15.

if sy-subrc <> 0 or l_exit_code <> 0.

perform sub_log_error using 'E01' text-m56 wc_report_file c_x c_x c_x.

endif.

You can also use GUI_UPLOAD and download FM

Hope this’ll give you idea!!

<b>P.S award the points.</b>

Good luck

Thanks

Saquib Khan

"Some are wise and some are otherwise"

nablan_umar
Active Contributor
0 Kudos

Hi Raymond,

This file looks like an Idoc file. So you may need to be careful when combining them since they contain segments that need to be in sequence. Anyhow, to concatenate 2 PC files, you can upload each using GUI_UPLOAD function module, which will store into an internal table. Then append the second table to the first one using APPEND command. Then download the file using function module GUI_DOWNLOAD.

0 Kudos

Hi Umar ,

Will the Gui_upload/dload ...also work for the files on Application Server ??

0 Kudos

No, you will need to use OPEN DATASET, READ DATASET, TRANSFER DATASET.

Regards,

Rich Heilman

0 Kudos

GUI_UPLOAD and DOWNLOAD only work for PC file. For Application Server file, use OPEN DATASET, READ DATASET, and TRANSFER commands

0 Kudos

Here is a sample program.



report zrich_0001.

parameters: d1 type localfile default '/usr/sap/TST/SYS/Data1.txt',
            d2 type localfile default '/usr/sap/TST/SYS/Data2.txt',
            d3 type localfile default '/usr/sap/TST/SYS/Data2.txt'.


data: itab1 type table of string with header line.
data: itab2 type table of string with header line.
data: itab3 type table of string with header line.

start-of-selection.

* Upload first one.
  open dataset d1 for input in text mode.
  if sy-subrc = 0.
    do.
      read dataset d1 into itab1.
      if sy-subrc <> 0.
        exit.
      endif.
      append itab1.
    enddo.
  endif.
  close dataset d1.

* Upload second one
  open dataset d2 for input in text mode.
  if sy-subrc = 0.
    do.
      read dataset d2 into itab2.
      if sy-subrc <> 0.
        exit.
      endif.
      append itab2.
    enddo.
  endif.
  close dataset d2.

* Write both to third file
  open dataset d3 for output in text mode.
  loop at itab1.
    transfer itab1 to d3.
  endloop.
  loop at itab2.
    transfer itab2 to d3.
  endloop.
  close dataset d3.

* Delete first two datasets.
  delete dataset d1.
  delete dataset d2.

Regards,

Rich Heilman

ferry_lianto
Active Contributor
0 Kudos

Hi Raymond,

You can do something like this.

OPEN DATASET <file1> FOR INPUT  IN TEXT MODE. 
OPEN DATASET <file2> FOR OUTPUT IN TEXT MODE. 

DO. 
  READ DATASET <file1> INTO <workarea>. 
  IF SY-SUBRC = 0. 
    ASSIGN <workarea> TO CASTING. 
    TRANSFER TO <file2>. 
  ELSE. 
    EXIT. 
  ENDIF. 
ENDDO. 

CLOSE DATASET <file1>.
CLOSE DATASET <file2>.

Hope this will give you an idea.

Regarsd,

Ferry Lianto