Sample ABAP Program for Application server File

Sample ABAP Program for Application server File

ABAP Program for Application server File is a sample SAP Custom Program to give you an example how file are handled on Application server.

The following ABAP program check if a file exists and create a new file on Application Server .

Sample ABAP Program for Application server File

The main points from this ABAP Sample code are:

Search Help for SAP Folder can be performed with the standard function module /SAPDMC/LSM_F4_SERVER_FILE.

Checking if a file exists on Application server can be do with OPEN DATASET file FOR INPUT IN BINARY MODE.

A sample OPEN DATASET file for OUTPUT to write file on Back-end.

REPORT  ZFILE_CREATION_APP.
*&---------------------------------------------------------------------*
*& Create file on Application Server
*& if the file exist, it will be deleted and created with new content
*&---------------------------------------------------------------------*

TABLES : kna1.
DATA: lv_file(255).
DATA: lt_kna1            TYPE TABLE OF kna1.
FIELD-SYMBOLS:  LIKE LINE OF lt_kna1.

"-----------------------------------------"
" Selection Screen
"-----------------------------------------"
SELECT-OPTIONS: s_kunnr FOR kna1-kunnr.
" File Path on Application Server
PARAMETERS: p_path TYPE btcxpgpar.

"-----------------------------------------"
" Help Search for SAP Folder
"-----------------------------------------"

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
  DATA: c_fnh_mask TYPE dxfields-filemask VALUE '*',
        search_dir TYPE dxfields-longpath .

  CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
    EXPORTING
      directory        = search_dir
      filemask         = c_fnh_mask
    IMPORTING
      serverfile       = p_path
    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.

  "-----------------------------------------"
  " Processing
  "-----------------------------------------"

START-OF-SELECTION.

  SELECT * FROM kna1 INTO TABLE lt_kna1
    WHERE kunnr IN s_kunnr[].
  IF sy-subrc NE 0 .
    RETURN.
  ENDIF.

  " Build FineName
  CONCATENATE p_path '\' 'Customer' INTO lv_file.
  REPLACE ALL OCCURRENCES OF '\\' IN lv_file WITH '\'.

  " Check if File exists
  OPEN DATASET  lv_file  FOR INPUT IN BINARY MODE.
  IF sy-subrc EQ 0.
    " If File Exists -> Delete it
    CLOSE DATASET   lv_file.
    DELETE DATASET  lv_file.
    CLOSE DATASET   lv_file.
  ENDIF.

  " Open file for Output
  OPEN DATASET  lv_file  FOR OUTPUT IN BINARY MODE.
  IF sy-subrc NE 0 .
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
     WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  " Transfer Data to file
  LOOP AT lt_kna1 ASSIGNING .
    TRANSFER  TO  lv_file .
  ENDLOOP.

  " Close File
  CLOSE DATASET  lv_file.

END-OF-SELECTION.
  WRITE: lv_file , ' is created' .

This ABAP Program will retrieve a list of Customer from KNA1 and create a file on Application server with the SAP Customer List.

Sample ABAP Program for Application server File

Find the OPEN DATASET documentation on help.sap.com