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.

Related Post

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: <fs_kna1> 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 <fs_kna1>.
    TRANSFER <fs_kna1> 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.

Find the OPEN DATASET documentation on help.sap.com

Share
Published by
John
Tags: ABAP Sample

Recent Posts

The Most Important SAP ISU Tables

SAP ISU Tables for Technical master data, for Billing Master Data, Billing and invoicing Tables?…

December 22, 2020

SAP Fiori 3 UX and Design of SAP Fiori Apps for SAP S/4HANA, SAP TechEd Lecture

SAP Fiori 3 UX and Design of SAP Fiori Apps for SAP S/4HANA, SAP TechEd…

November 18, 2020

The Main SAP Dunning Transaction Codes

Dunning is the business practice of informing a customer of past due payment. Dunning letters…

November 28, 2019

SAP Accounts Payable Tcodes & Accounts Receivable Tcodes ( SAP AP Tcodes & SAP AR Tcodes)

SAP AP Tcodes & SAP AR Tcodes: House Banks Tcodes, Advance Payments or Down Payments,…

August 1, 2019

The Most Important SAP Payment Terms Tables (ZTERM, Text…)

What are the main SAP Payment Terms Tables ? What are the related Tables in…

October 21, 2018

The most Important SAP Work Center Tables in SAP PP

Work center consists of master data related to routing of products. It contains data related…

October 21, 2018