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

Reading file from an external folder.

Former Member
0 Likes
3,030

Iam tring to read an external file from the C:\ drive. Given below is the code.

DATA LV_XLS(100) TYPE C.

DATA LV_CONTENT TYPE XSTRING.

LV_XLS = 'C:\XML\Report.xls'

READ DATASET LV_XLS INTO LV_CONTENT.

But when i execute iam getting an error - FILE NOT FOUND. How do i read file from an external folder. My requirement is to strictly read from an external folder. Please help.

If this is not possible please advice as to how the file could be moved to the SAP root directory. Iam not able to locate the root.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,870

The t-code for copying files to Application layer to Presentation layer and vice versa is CG3Y and CG3Z.

And the following codes demonstrates how to access that file:

PARAMETERS: P_DSET TYPE STRING
                    DEFAULT 'E:\usr\sap\EC4\DVEBMGS00\work\rajan.txt'.

OPEN DATASET P_DSET FOR INPUT IN TEXT MODE ENCODING DEFAULT.

  IF SY-SUBRC = 4.
    MESSAGE 'ERROR IN FILE OPENING' TYPE 'E'.
  ENDIF.


  DO.  "This depends on your requirement.
    READ DATASET P_DSET INTO D_STRING.
    IF SY-SUBRC <> 0.
      EXIT.

    ELSEIF SY-SUBRC = 0.
       .........................
             "Now whatever you need to do with the data.
       ..........................
    ENDIF.
  ENDDO.

CLOSE DATASET P_DSET.

Iam tring to read an external file from the C:\ drive. Given below is the code.

DATA LV_XLS(100) TYPE C.

DATA LV_CONTENT TYPE XSTRING.

LV_XLS = 'C:\XML\Report.xls'

READ DATASET LV_XLS INTO LV_CONTENT.

But when i execute iam getting an error - FILE NOT FOUND. How do i read file from an external folder. My requirement is to strictly read from an external folder. Please help.

If this is not possible please advice as to how the file could be moved to the SAP root directory. Iam not able to locate the root.

8 REPLIES 8
Read only

Former Member
0 Likes
1,870

Hi,

Follow the below steps:

1) To open a file for reading, use the FOR INPUT addition to the OPEN DATASET statement

The file must already exist, otherwise, the system sets SY-SUBRC to 8, and ignores the statement.

OPEN DATASET <dsn> FOR INPUT.

2)To read data from a file on the application server, use the READ DATASET statement.

READ DATASET <dsn> INTO <f> [LENGTH <len>].

Regards

Shiva

Read only

Former Member
0 Likes
1,870

hi,

This is the code to read a excel file from the local system.

Read data set you are using in your code corresponds to files on application server thats the reason its throwing an error.



*"--------------------------------------------------------------------*
* Parameter variable declaration for browsing the file location       *
*"--------------------------------------------------------------------*
PARAMETERS:
  p_file TYPE ibipparms-path OBLIGATORY.
data:
it_text TYPE truxs_t_text_data .


AT SELECTION-SCREEN  ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
   EXPORTING
     program_name        = syst-cprog
*   DYNPRO_NUMBER       = SYST-DYNNR
     field_name          = ' '
   IMPORTING
     file_name           = p_file.

START-OF-SELECTION.

  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
*   I_FIELD_SEPERATOR          =
*   I_LINE_HEADER              =
      i_tab_raw_data             = it_text
      i_filename                 = p_file
    TABLES
      i_tab_converted_data       = t_cust
  EXCEPTIONS
    conversion_failed          = 1
    OTHERS                     = 2
           .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

This is to read a text file from local system.


  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
    filename                      =  'D:\FILEINTERFACE\file.TXT'
*    FILETYPE                      = 'ASC'
     has_field_separator           = 'X'
*    HEADER_LENGTH                 = 0
*    READ_BY_LINE                  = 'X'
*    DAT_MODE                      = ' '
*    CODEPAGE                      = ' '
*    IGNORE_CERR                   = ABAP_TRUE
*    REPLACEMENT                   = '#'
*  IMPORTING
*    FILELENGTH                    =
*    HEADER                        =
    TABLES
      data_tab                      = t_kna1
*  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. 

PS. In both of the above mentioned function modules make sure that the file structure is similar to the internal tables used.

Thanks

Sharath

Read only

Former Member
0 Likes
1,870

This message was moderated.

Read only

Former Member
0 Likes
1,871

The t-code for copying files to Application layer to Presentation layer and vice versa is CG3Y and CG3Z.

And the following codes demonstrates how to access that file:

PARAMETERS: P_DSET TYPE STRING
                    DEFAULT 'E:\usr\sap\EC4\DVEBMGS00\work\rajan.txt'.

OPEN DATASET P_DSET FOR INPUT IN TEXT MODE ENCODING DEFAULT.

  IF SY-SUBRC = 4.
    MESSAGE 'ERROR IN FILE OPENING' TYPE 'E'.
  ENDIF.


  DO.  "This depends on your requirement.
    READ DATASET P_DSET INTO D_STRING.
    IF SY-SUBRC <> 0.
      EXIT.

    ELSEIF SY-SUBRC = 0.
       .........................
             "Now whatever you need to do with the data.
       ..........................
    ENDIF.
  ENDDO.

CLOSE DATASET P_DSET.

Read only

Former Member
0 Likes
1,870

Hi,

Try like this...



open dataset LV_XLS for input in text mode encoding default.
  if sy-subrc = 0.
    do.
      read dataset LV_XLS into w_input-wa_string.
      if sy-subrc <> 0.
        exit.
      endif.
      append w_input to t_input.
    enddo.
    close dataset LV_XLS.
  endif.

Hope its helps

Read only

awin_prabhu
Active Contributor
0 Likes
1,870

Hi friend,

Try using 'GUI_UPLOAD' for reading an excel file into internal table.

REPORT zdemodownload .

*Types

TYPES: BEGIN OF g_r_mara,

matnr LIKE mara-matnr,

ersda LIKE mara-ersda,

laeda LIKE mara-laeda,

mtart LIKE mara-mtart,

mbrsh LIKE mara-mbrsh,

END OF g_r_mara.

*Data

DATA: g_t_mara1 TYPE TABLE OF g_r_mara,

filename TYPE string,

g_r_wa TYPE g_r_mara.

*Tables

TABLES: mara.

*Selection Screen

SELECT-OPTIONS: s_matnr FOR mara-matnr.

START-OF-SELECTION.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = 'C:\Testing.xls'

FILETYPE = 'ASC'

HAS_FIELD_SEPARATOR = 'X'

tables

data_tab = g_t_mara1

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

WRITE: / 'Uploaded data' COLOR = 1.

WRITE:/.

CLEAR g_r_wa.

LOOP AT g_t_mara1 INTO g_r_wa.

WRITE:/ g_r_wa-matnr, g_r_wa-ersda, g_r_wa-laeda, g_r_wa-mtart, g_r_wa-mbrsh.

ENDLOOP.

Have your excel file in C: as Testing.xls

Thanks..

Edited by: Guest77 on Feb 16, 2009 2:18 PM

Read only

Former Member
0 Likes
1,870

Basically iam trying to zip 2 files. The code is as given below.

DATA:

LV_FILE_LENGTH TYPE I ,

LV_CONTENT TYPE XSTRING,

LV_ZIP_CONTENT TYPE XSTRING,

LO_ZIP TYPE REF TO CL_ABAP_ZIP.

DATA

LV_XLS(100) VALUE 'C:\XML\Report.xls',

LV_XML(100) VALUE 'C:\XML\2006_1299.xml'.

CREATE OBJECT LO_ZIP.

CLEAR LV_CONTENT .

OPEN DATASET LV_ FOR INPUT IN TEXT MODE ENCODING DEFAULT MESSAGE LV_MESSG.

IF SY-SUBRC = 0.

READ DATASET LV_XML INTO LV_CONTENT .

CLOSE DATASET LV_XML.

LO_ZIP->ADD( NAME = '2006_1299.xml' CONTENT = LV_CONTENT ).

ENDIF.

CLEAR LV_CONTENT .

OPEN DATASET LV_XLS FOR INPUT IN BINARY MODE.

IF SY-SUBRC = 0.

READ DATASET LV_XLS INTO LV_CONTENT .

CLOSE DATASET LV_XLS.

LO_ZIP->ADD( NAME = 'Report.xls' CONTENT = LV_CONTENT ).

ENDIF.

LV_ZIP_CONTENT = LO_ZIP->SAVE( ).

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

EXPORTING

BUFFER = LV_ZIP_CONTENT

IMPORTING

OUTPUT_LENGTH = LV_FILE_LENGTH

TABLES

BINARY_TAB = LT_DATA.

OPEN DATASET LV_ZIP FOR OUTPUT IN BINARY MODE.

IF SY-SUBRC = 0.

LOOP AT LT_DATA INTO LS_DATA.

TRANSFER LS_DATA TO LV_ZIP.

ENDLOOP.

CLOSE DATASET LV_ZIP.

MOVE LV_ZIP TO REPORT_360.

ENDIF.

But iam not able to read the file for processing. Is there a work around for this - to zip the files in the external system directory.

Read only

Former Member
0 Likes
1,870

Hi,

Use this FM

FILE_READ_AND_CONVERT_SAP_DATA

Regards

Kiran