Application Development and Automation 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: 
Read only

upload text files

Former Member
0 Likes
6,062

Hi,

Can anyone help me with the source code to upload and compare 2 text files?

it urgent plz.

regards.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
4,070

report zexternalfile2 .

tables : zzpublisher,

sscrfields.

data: it_publisher like standard table of zzpublisher,

wa_publisher like zzpublisher.

call function 'GUI_UPLOAD'

exporting

filename = 'C:\externalfiles\aravind.txt'

  • FILETYPE = 'ASC'

  • HAS_FIELD_SEPARATOR = ' '

  • HEADER_LENGTH = 0

  • READ_BY_LINE = 'X'

  • DAT_MODE = ' '

  • IMPORTING

  • FILELENGTH =

  • HEADER =

tables

data_tab = it_publisher

  • EXCEPTIONS

  • FILE_OPEN_ERROR = 1

  • FILE_READ_ERROR = 2

  • NO_BATCH = 3

  • GUI_REFUSE_FILETRANSFER = 4

  • INVALID_TYPE = 5

  • NO_AUTHORITY = 6

  • UNKNOWN_ERROR = 7

  • BAD_DATA_FORMAT = 8

  • HEADER_NOT_ALLOWED = 9

  • SEPARATOR_NOT_ALLOWED = 10

  • HEADER_TOO_LONG = 11

  • UNKNOWN_DP_ERROR = 12

  • ACCESS_DENIED = 13

  • DP_OUT_OF_MEMORY = 14

  • DISK_FULL = 15

  • DP_TIMEOUT = 16

  • OTHERS = 17

.

if sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

endif.

loop at it_publisher into wa_publisher.

write: / wa_publisher-publishercode , wa_publisher-publishername ,

wa_publisher-city.

endloop.

Hi,

Can anyone help me with the source code to upload and compare 2 text files?

it urgent plz.

regards.

9 REPLIES 9
Read only

Former Member
0 Likes
4,070

Hi Martina,

You can use GUI_UPLOAD to upload the text file, but it is not easy to compare the two files using the ABAP. You should explore other optiosn instead of comparing them in ABAP.

Regards,

Atish

Read only

Former Member
0 Likes
4,070
*& Report  ZUPLOADTAB                                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Example of Uploading tab delimited file                             *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  zuploadtab                    .

PARAMETERS: p_infile  LIKE rlgrap-filename
                        OBLIGATORY DEFAULT  '/usr/sap/'..

*DATA: ld_file LIKE rlgrap-filename.
DATA: gd_file type string.

*Internal tabe to store upload data
TYPES: BEGIN OF t_record,
    name1 LIKE pa0002-vorna,
    name2 LIKE pa0002-name2,
    age   TYPE i,
    END OF t_record.
DATA: it_record TYPE STANDARD TABLE OF t_record INITIAL SIZE 0,
      wa_record TYPE t_record.

*Internal table to upload data into
DATA: BEGIN OF it_datatab OCCURS 0,
  row(500) TYPE c,
 END OF it_datatab.

*Text version of data table
TYPES: BEGIN OF t_uploadtxt,
  name1(10) TYPE c,
  name2(15) TYPE c,
  age(5)  TYPE c,
 END OF t_uploadtxt.
DATA: wa_uploadtxt TYPE t_uploadtxt.


*String value to data in initially.
DATA: wa_string(255) TYPE c.

CONSTANTS: con_tab TYPE x VALUE '09'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_infile.
  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            def_filename     = p_infile
            mask             = ',*.txt.'
            mode             = 'O'
            title            = 'Upload File'(078)
       IMPORTING
            filename         = p_infile
       EXCEPTIONS
            inv_winsys       = 1
            no_batch         = 2
            selection_cancel = 3
            selection_error  = 4
            OTHERS           = 5.


START-OF-SELECTION.
  gd_file = p_infile.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = gd_file
      has_field_separator     = 'X'  "file is TAB delimited
    TABLES
      data_tab                = it_record
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
    IF sy-subrc NE 0.
      write: 'Error ', sy-subrc, 'returned from GUI_UPLOAD FM'.
      skip.
    endif.


END-OF-SELECTION.
*!! Text data is now contained within the internal table IT_RECORD

* Display report data for illustration purposes
LOOP AT it_record INTO wa_record.
  WRITE:/     sy-vline,
         (10) wa_record-name1, sy-vline,
         (10) wa_record-name2, sy-vline,
         (10) wa_record-age, sy-vline.
ENDLOOP.

reward points if it is usefull ..

Girish

Read only

Former Member
0 Likes
4,070

DATA: I_TAB TYPE STANDARD TABLE OF TY_ITAB WITH HEADER LINE.

DATA : GFILE(150).

DATA: G_FILE(150).

DATA: PATH(100).

DATA: DATE(8), DATE1(6).

DATA: RECORD TYPE STRING,

FILE TYPE STRING.

DATA: L_FILETAB TYPE FILETABLE,

L_FILETAB_H TYPE FILETABLE WITH HEADER LINE,

INITIAL_DIR_PATH TYPE STRING,

L_RC TYPE I.

SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

PARAMETERS: FNAME TYPE LOCALFILE OBLIGATORY.

SELECTION-SCREEN: END OF BLOCK B1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR FNAME.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG

EXPORTING

WINDOW_TITLE = 'Select the Asset Master Data file'

  • DEFAULT_EXTENSION =

  • DEFAULT_FILENAME =

FILE_FILTER = 'Text Files (.TXT)|.TXT|'

INITIAL_DIRECTORY = INITIAL_DIR_PATH

MULTISELECTION = SPACE

  • WITH_ENCODING =

CHANGING

FILE_TABLE = L_FILETAB

RC = L_RC

  • USER_ACTION =

  • FILE_ENCODING =

EXCEPTIONS

FILE_OPEN_DIALOG_FAILED = 1

CNTL_ERROR = 2

ERROR_NO_GUI = 3

NOT_SUPPORTED_BY_GUI = 4

OTHERS = 5

.

IF SY-SUBRC <> 0.

WRITE:/ 'Error while selecting the input file'.

ELSE.

LOOP AT L_FILETAB INTO L_FILETAB_H.

FNAME = L_FILETAB_H-FILENAME.

EXIT.

ENDLOOP.

ENDIF.

START-OF-SELECTION.

PERFORM READ_FILE_DATA.

&----


*& Form READ_FILE_DATA

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM READ_FILE_DATA .

DATA: VFILE TYPE STRING.

DATA: FL(1).

VFILE = FNAME.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME = VFILE

FILETYPE = 'ASC'

HAS_FIELD_SEPARATOR = 'X'

  • HEADER_LENGTH = 0

  • READ_BY_LINE = 'X'

  • DAT_MODE = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • CHECK_BOM = ' '

  • VIRUS_SCAN_PROFILE =

  • IMPORTING

  • FILELENGTH =

  • HEADER =

TABLES

DATA_TAB = I_TAB

EXCEPTIONS

FILE_OPEN_ERROR = 1

FILE_READ_ERROR = 2

NO_BATCH = 3

GUI_REFUSE_FILETRANSFER = 4

INVALID_TYPE = 5

NO_AUTHORITY = 6

UNKNOWN_ERROR = 7

BAD_DATA_FORMAT = 8

HEADER_NOT_ALLOWED = 9

SEPARATOR_NOT_ALLOWED = 10

HEADER_TOO_LONG = 11

UNKNOWN_DP_ERROR = 12

ACCESS_DENIED = 13

DP_OUT_OF_MEMORY = 14

DISK_FULL = 15

DP_TIMEOUT = 16

OTHERS = 17

.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ENDFORM. " READ_FILE_DATA

Read only

Former Member
0 Likes
4,071

report zexternalfile2 .

tables : zzpublisher,

sscrfields.

data: it_publisher like standard table of zzpublisher,

wa_publisher like zzpublisher.

call function 'GUI_UPLOAD'

exporting

filename = 'C:\externalfiles\aravind.txt'

  • FILETYPE = 'ASC'

  • HAS_FIELD_SEPARATOR = ' '

  • HEADER_LENGTH = 0

  • READ_BY_LINE = 'X'

  • DAT_MODE = ' '

  • IMPORTING

  • FILELENGTH =

  • HEADER =

tables

data_tab = it_publisher

  • EXCEPTIONS

  • FILE_OPEN_ERROR = 1

  • FILE_READ_ERROR = 2

  • NO_BATCH = 3

  • GUI_REFUSE_FILETRANSFER = 4

  • INVALID_TYPE = 5

  • NO_AUTHORITY = 6

  • UNKNOWN_ERROR = 7

  • BAD_DATA_FORMAT = 8

  • HEADER_NOT_ALLOWED = 9

  • SEPARATOR_NOT_ALLOWED = 10

  • HEADER_TOO_LONG = 11

  • UNKNOWN_DP_ERROR = 12

  • ACCESS_DENIED = 13

  • DP_OUT_OF_MEMORY = 14

  • DISK_FULL = 15

  • DP_TIMEOUT = 16

  • OTHERS = 17

.

if sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

endif.

loop at it_publisher into wa_publisher.

write: / wa_publisher-publishercode , wa_publisher-publishername ,

wa_publisher-city.

endloop.

Read only

0 Likes
4,070

Do i naee 2 internal tables for 2 files?

Read only

0 Likes
4,070

Yes Martina,

You will require two tables for two files and you need to call GUI_UPLOAD twice.

Regards,

Atish

Read only

Former Member
0 Likes
4,070

Here is the program for uploading 2 file by comapring the 2 files internal table .

*& Report  ZUPLOADTAB                                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Example of Uploading tab delimited file                             *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  zuploadtab                    .
tables :  rlgrap .
parameter : p_infil1 like rlgrap-filename
                        OBLIGATORY DEFAULT  '/usr/sap/'..

parameter : p_infil2 like rlgrap-filename
                        OBLIGATORY DEFAULT  '/usr/sap/'..

*DATA: ld_file LIKE rlgrap-filename.
DATA: gd_file type string , X  type  i  value   1.

*Internal tabe to store upload data
TYPES: BEGIN OF t_record,
    name1 LIKE pa0002-vorna,
    name2 LIKE pa0002-name2,
    age   TYPE i,
    END OF t_record.
DATA: it_record TYPE STANDARD TABLE OF t_record INITIAL SIZE 0,
      wa_record TYPE t_record.

*Internal table to upload data into
DATA: BEGIN OF it_datatab OCCURS 0,
  row(500) TYPE c,
 END OF it_datatab.

*Text version of data table
TYPES: BEGIN OF t_uploadtxt,
  name1(10) TYPE c,
  name2(15) TYPE c,
  age(5)  TYPE c,
 END OF t_uploadtxt.
DATA: wa_uploadtxt TYPE t_uploadtxt.


*String value to data in initially.
DATA: wa_string(255) TYPE c.

CONSTANTS: con_tab TYPE x VALUE '09'.

*If you have Unicode check active in program attributes then you will
*need to declare constants as follows:

*class cl_abap_char_utilities definition load.
*constants:
*    con_tab  type c value cl_abap_char_utilities=>HORIZONTAL_TAB.




************************************************************************
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_INFILE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_infil1.


  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            def_filename     = p_infil1
            mask             = ',*.txt.'
            mode             = 'O'
            title            = 'Upload File'(078)
       IMPORTING
            filename         = p_infil1
       EXCEPTIONS
            inv_winsys       = 1
            no_batch         = 2
            selection_cancel = 3
            selection_error  = 4
            OTHERS           = 5.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_infil2.


  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            def_filename     = p_infil2
            mask             = ',*.txt.'
            mode             = 'O'
            title            = 'Upload File'(078)
       IMPORTING
            filename         = p_infil2
       EXCEPTIONS
            inv_winsys       = 1
            no_batch         = 2
            selection_cancel = 3
            selection_error  = 4
            OTHERS           = 5.


************************************************************************
*START-OF-SELECTION
START-OF-SELECTION.

<b>do 2 times .

if X = '1' .

gd_file = p_infil1.

elseif X = '2' .

gd_file = p_infil2.

ENDIF.

perform twofiles .

X = X + 1 .

enddo .</b>


FORM TWOFILES   .
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = gd_file
      has_field_separator     = 'X'  "file is TAB delimited
    TABLES
      data_tab                = it_record
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
    IF sy-subrc NE 0.
      write: 'Error ', sy-subrc, 'returned from GUI_UPLOAD FM'.
      skip.
    endif.
ENDFORM.                    " TWOFILES

************************************************************************
*END-OF-SELECTION
END-OF-SELECTION.

<b>SORT it_record BY name1 name2.

DELETE ADJACENT DUPLICATES FROM it_record COMPARING name1 name2 .</b>


LOOP AT it_record INTO wa_record.
  WRITE:/     sy-vline,
         (10) wa_record-name1, sy-vline,
         (10) wa_record-name2, sy-vline,
         (10) wa_record-age, sy-vline.
ENDLOOP.

reward points if it is usefull ..

Girish

Read only

Former Member
0 Likes
4,070

Thanks a lot to all,

Points will be awarded.

Read only

Former Member
0 Likes
4,070

not required <b> 2 internal table ...</b> only <b> 1 is Enough</b>

<b>you can see the last reply i used only one internal table and deleted by Comparing data in the internal table and DELETED .</b>

reward points if it is usefull ...

Girish