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 application server

Former Member
0 Likes
1,275

HI Experts,

For upload data we are reading files from local disk only. But now we have to read from application server. How can I read a file from the application server? For this what I need? Did I need application server I.P address & the folder path? Please explain me detail.

Thanks & Regards

Rajendra

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,195

Hi,

here a short example to read/write:

TABLES: MARA.

*

DATA: DATEI_A(30) TYPE C VALUE '/transfer/sap/matnr.txt'.

*

TYPES: BEGIN OF IMARA,

MATNR LIKE MARA-MATNR,

MTART LIKE MARA-MATNR,

END OF IMARA.

*

DATA: ITAB TYPE TABLE OF IMARA WITH HEADER LINE.

*

START-OF-SELECTION.

*

SELECT MATNR MTART INTO TABLE ITAB FROM MARA UP TO 10 ROWS.

*

PERFORM DATEI_AUSGEBEN.

PERFORM DATEI_EINLESEN.

*

************************************************************************

FORM DATEI_AUSGEBEN.

*

OPEN DATASET DATEI_A FOR OUTPUT IN TEXT MODE.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

LOOP AT ITAB.

*

TRANSFER ITAB TO DATEI_A.

*

ENDLOOP.

*

CLOSE DATASET DATEI_A.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

ENDFORM. "DATEI_AUSGEBEN

************************************************************************

FORM DATEI_EINLESEN.

*

*

OPEN DATASET DATEI_A FOR INPUT IN TEXT MODE.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

DO.

*

READ DATASET DATEI_A INTO ITAB_READ.

*

IF SY-SUBRC <> 0. EXIT. ENDIF.

*

APPEND ITAB_READ.

*

ENDDO.

*

CLOSE DATASET DATEI_A.

*

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

WRITE: / 'INPUT:'.

LOOP AT ITAB_READ. WRITE: / ITAB_READ. ENDLOOP.

*

ENDFORM. "DATEI_EINLESEN

Regards, Dieter

10 REPLIES 10
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,195

Here is an example of reading a comma delimited file from the appllicaton server. Notice the path used, there is no need to specify the IP or host, the system knows what it is running on, so you just need the path starting from the usr/ folder.

Then you use the DATASET statements to OPEN, READ and CLOSE the dataset.



report zrich_0001.

data: str type string.

data: begin of itab occurs 0,
      fld1(10) type c,
      fld2(10) type c,
      fld3(10) type c,
      end   of itab.

data:
      dsn(100) value '/usr/sap/TST/sys/test.txt'.

clear itab.  refresh itab.

* Read the data.
open dataset dsn for input in binary mode.
do.
  read dataset dsn into str.
  if sy-subrc = 0.
   split str at ',' into itab-fld1 itab-fld2 itab-fld3.
    append itab.
  else.
    exit.
  endif.
enddo.
close dataset dsn.


Loop at itab.
  write:/ itab-fld1, itab-fld2, itab-fld3.
endloop.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,196

Hi,

here a short example to read/write:

TABLES: MARA.

*

DATA: DATEI_A(30) TYPE C VALUE '/transfer/sap/matnr.txt'.

*

TYPES: BEGIN OF IMARA,

MATNR LIKE MARA-MATNR,

MTART LIKE MARA-MATNR,

END OF IMARA.

*

DATA: ITAB TYPE TABLE OF IMARA WITH HEADER LINE.

*

START-OF-SELECTION.

*

SELECT MATNR MTART INTO TABLE ITAB FROM MARA UP TO 10 ROWS.

*

PERFORM DATEI_AUSGEBEN.

PERFORM DATEI_EINLESEN.

*

************************************************************************

FORM DATEI_AUSGEBEN.

*

OPEN DATASET DATEI_A FOR OUTPUT IN TEXT MODE.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

LOOP AT ITAB.

*

TRANSFER ITAB TO DATEI_A.

*

ENDLOOP.

*

CLOSE DATASET DATEI_A.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

ENDFORM. "DATEI_AUSGEBEN

************************************************************************

FORM DATEI_EINLESEN.

*

*

OPEN DATASET DATEI_A FOR INPUT IN TEXT MODE.

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

DO.

*

READ DATASET DATEI_A INTO ITAB_READ.

*

IF SY-SUBRC <> 0. EXIT. ENDIF.

*

APPEND ITAB_READ.

*

ENDDO.

*

CLOSE DATASET DATEI_A.

*

IF SY-SUBRC NE 0. MESSAGE E001. STOP. ENDIF.

*

WRITE: / 'INPUT:'.

LOOP AT ITAB_READ. WRITE: / ITAB_READ. ENDLOOP.

*

ENDFORM. "DATEI_EINLESEN

Regards, Dieter

Read only

Former Member
Read only

Former Member
0 Likes
1,195

Hi,

*   Retrieve Data file from Application server(Upload from Unix)
DATA: i_file like rlgrap-filename value '/usr/sap/tmp/file.txt'.

    OPEN DATASET i_file FOR INPUT IN TEXT MODE.
    IF sy-subrc NE 0.
      MESSAGE e999(za) WITH 'Error opening file' i_file.
    ENDIF.
    DO.
*     Reads each line of file individually
      READ DATASET i_file INTO wa_datatab.
*     Perform processing here
*     .....   
    ENDDO.
CLOSE DATASET.

Read only

Former Member
0 Likes
1,195

Generally you need the following pattern in your code:

OPEN DATASET (which opens the specified file)

DO.

READ DATASET (which reads a record from a sequential file )

ENDDO.

CLOSE DATASET (which closes the specified file)

with suitable additions (see F1 help for details).

Regards,

Renata

Read only

Former Member
0 Likes
1,195

Hi Rajendra,

problem solved?

Regards, Dieter

Read only

0 Likes
1,195

HI Diter,

Thanks for your mail.

Instead of specifying the file name directly is it possible to select the file from the server folder?

Thanks & Regards

Rajendra

Read only

0 Likes
1,195

I think you can use the function module 'RZL_READ_DIR_LOCAL'. If you pass the directory name in the function call it will return a list fo files in that directory.

~Suresh

Read only

0 Likes
1,195

Hi Rajendra Prasad,

Assuming the PARAMETER u defined for string Application Server filename is p_pname, write the following code.

  • F4 help to select physical file from Appl Server

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_pname.

PERFORM get_unix_filename USING p_pname.

&----


*& Form GET_UNIX_FILENAME

&----


  • Subroutine to get the upload filename from Application Server based on

  • the user's selction on selection screen

----


  • -->P_PNAME Filename

----


FORM get_unix_filename USING p_pname TYPE filename-fileextern.

  • Local Internal tables

  • Structure to store list of all the directories in appl server

TYPES: BEGIN OF ty_dirtable,

lisel LIKE sy-lisel,

END OF ty_dirtable.

  • Local Variables and internal tables

DATA:

l_curline LIKE sy-lilli, "Line of Filename Selected

l_i_dirtable TYPE STANDARD TABLE OF ty_dirtable,

l_i_dirtab TYPE STANDARD TABLE OF abaplist,

l_i_return TYPE STANDARD TABLE OF ddshretval,

l_w_dirtable TYPE ty_dirtable,

l_w_return TYPE ddshretval.

  • Program to get all the SAP directories

SUBMIT rswatch0

EXPORTING LIST TO MEMORY

AND RETURN.

  • Clear and refresh

REFRESH l_i_dirtab.

CLEAR l_i_dirtab.

  • Get the list of directories from memory

CALL FUNCTION 'LIST_FROM_MEMORY'

TABLES

listobject = l_i_dirtab

EXCEPTIONS

not_found = 1

OTHERS = 2.

IF sy-subrc NE 0.

MESSAGE s000 WITH 'List is empty'(011).

EXIT.

ENDIF.

  • Display the directory list

CALL FUNCTION 'WRITE_LIST'

TABLES

listobject = l_i_dirtab

EXCEPTIONS

empty_list = 1

OTHERS = 2.

IF sy-subrc NE 0.

MESSAGE s000 WITH 'List is empty'(011).

EXIT.

ENDIF.

  • Count number of lines displayed on the list

DESCRIBE LIST NUMBER OF LINES l_curline INDEX 0.

  • Get the directories list

CLEAR: l_i_dirtable.

DO l_curline TIMES.

READ LINE sy-index.

l_w_dirtable-lisel = sy-lisel.

REPLACE ALL OCCURRENCES OF '|' IN l_w_dirtable-lisel WITH space.

IF l_w_dirtable-lisel(04) EQ 'DIR_'.

APPEND l_w_dirtable TO l_i_dirtable.

CLEAR l_w_dirtable.

ENDIF.

ENDDO.

  • Get the filename

CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'

EXPORTING

retfield = 'P_PNAME'

value_org = 'S'

TABLES

value_tab = l_i_dirtable

return_tab = l_i_return

EXCEPTIONS

parameter_error = 1

no_values_found = 2

OTHERS = 3.

IF sy-subrc <> 0.

CLEAR v_message.

PERFORM get_message CHANGING v_message.

MESSAGE e000 WITH v_message.

ENDIF.

  • If user cancelled or not file name is selected,

IF l_i_return IS NOT INITIAL.

READ TABLE l_i_return INTO l_w_return INDEX 1.

IF l_w_return-fieldval IS NOT INITIAL.

MOVE l_w_return-fieldval+20 TO p_pname.

CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'

EXPORTING

directory = p_pname

filemask = ' '

IMPORTING

serverfile = p_pname

EXCEPTIONS

canceled_by_user = 1

OTHERS = 2.

IF sy-subrc <> 0.

CLEAR v_message.

PERFORM get_message CHANGING v_message.

MESSAGE e000 WITH v_message.

ENDIF.

ENDIF.

ENDIF.

ENDFORM. " GET_UNIX_FILENAME

The above code will help you to select a filename from the Application Server.

Hope this solves your problem.

Enjoy SAP.

Rajasekhar

Read only

ferry_lianto
Active Contributor
0 Likes
1,195

Hi,

Please try this.


parameters : p_file like rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

DATA: c_fnh_mask TYPE dxfields-filemask VALUE '*.*',
  search_dir TYPE dxfields-longpath VALUE '/sapglobal/users',
  file_path LIKE dxfields-longpath.
 
  CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
    EXPORTING
      i_location_flag = 'A'
      i_server        = ' '
      i_path          = search_dir
      filemask        = c_fnh_mask
      fileoperation   = 'R'
    IMPORTING
      o_path          = file_path
    EXCEPTIONS
      rfc_error       = 1
      OTHERS          = 2.
  IF sy-subrc EQ 0.
    p_file = file_path.
  ENDIF.

or 

  call function '/SAPDMC/LSM_F4_SERVER_FILE'
    exporting
      directory        = ' '
      filemask         = ' '
    importing
      serverfile       = p_file
    exceptions
      canceled_by_user = 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.

Regards,

Ferry Lianto