SAP ABAP Working with files

作者:郑德鼎 约 6 分钟阅读 更新日期:2024-02-22 2 年前更新 标签:ABAP, 开发
目录

Working with files

Skip to end of metadata

Created by Sandra Rossi, last modified on Nov 04, 2009

Go to start of metadata

Table of contents

Introduction

Working with files on the application server

How to create or write to a file

How to read a file

Checking file existence

How to get list of files in a directory

Is there a search help for selecting a file

Best practice

Miscellaneous

Working with files on presentation server

How to create or write a file

How to read a file

Checking file existence

How to get list of files in a directory

Is there a search help for selecting a file or directory

Is it possible to work in background?

Miscellaneous

Copying from server to frontend or vice versa

File compression (zip)

Working with special files

Excel file types

OLE/DOI for Office native files (Excel, Word, etc.)

Introduction

In this article, only general directions are given. You may find snippets here

Files are handled differently when they are on the application server, or on the presentation server

Files may be processed in 2 modes, either text (in a given encoding), or binary (generated by a software like Microsoft Office, Adobe Acrobat, etc.) From ABAP, you may directly write and read text files, while binary files can only be written and read by the software. The mode is chosen when opening the file.

Working with files on the application server

Transaction code AL11 takes you to the application server in which you find the sap directories and the files inside the directories.

The configure button on the main screen can be used to create and manage links to existing directories.

Files may be processed in 2 ways, decided in OPEN DATASET statement: either as text files with a given encoding (IN TEXT MODE, ENCODING and CODE PAGE additions), or as binary files (IN BINARY MODE addition).

How to create or write to a file

Use ABAP statements OPEN DATASET with FOR OUTPUT keyword, TRANSFER, CLOSE DATASET.

How to read a file

Use ABAP statements OPEN DATASET with FOR INPUT keyword, READ DATASET and CLOSE DATASET.

Checking file existence

either OCS_GET_FILE_INFO function module

or "OPEN DATASET" ABAP statement

How to get list of files in a directory

To read the names of files present in a particular path on the application server we may use SUBST_GET_FILE_LIST or EPS_GET_DIRECTORY_LISTING function modules.

Is there a search help for selecting a file

F4_DXFILENAME_TOPRECURSION function module may be used to display a search help to choose a file

Snippet

Best practice

Instead of hardcoding the file names, it is possible to define generic names which are called "logical filenames". They may be defined using FILE transaction code, and used using FILE_GET_NAME function module.

Miscellaneous

sdn contribution : ABAP Server Side File Access, by Thomas Jung (class ZCL_ES_SERVER_FILE_ACCESS)

Note 752859: sapiconv - a tool for converting the encoding of files

Working with files on presentation server

How to create or write a file

either GUI_DOWNLOAD function module

Snippet

or CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD method

Snippet

Note: DOWNLOAD and WS_DOWNLOAD function modules are obsolete

How to read a file

either GUI_UPLOAD function module

Snippet

or CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD method

Note: UPLOAD and WS_UPLOAD function modules are obsolete

Checking file existence

either DX_FILE_EXISTENCE_CHECK function module

or CL_GUI_FRONTEND_SERVICES=>FILE_EXIST method

How to get list of files in a directory

either TMP_GUI_DIRECTORY_LIST_FILES function module

or CL_GUI_FRONTEND_SERVICES=>DIRECTORY_LIST_FILES method

Is there a search help for selecting a file or directory

For selecting a *file", you may use

CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG method (Basis, 4.6 and after): displays list as a popup with title "Open", and returns the name of selected file

CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG method (Basis, 4.6 and after): displays list as a popup with title "Save", and returns the name of selected file

Snippet

WS_FILENAME_GET function module (Basis, any release)

F4_FILENAME function module (R/3 and ECC only)

For selecting a directory, you may use

CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE method

Is it possible to work in background?

No, this functionality is not supplied because presentation servers may be switched off at any time without guarantee (see Note 7925 - Graphics, Upload, Download do not work in backgrnd).

Workarounds may be

Work with files on application server. The user may copy files from or to the application server when he is connected

If you take decisions to make the presentation server work as a server (always the same computer, and it would be switched on), consider it is an RFC server: see SDN article Communication to Presentation Server in Background Mode for more information.

Miscellaneous

CL_GUI_FRONTEND_SERVICES class provides many other methods about files

Copying from server to frontend or vice versa

CL_GUI_FRONTEND_SERVICES=>FILE_COPY method

SAP commander

R/3 and ECC: transaction codes CG3Y (from server to frontend) and CG3Z (from frontend to server)

File compression (zip)

Class CL_ABAP_ZIP may be used to read or write a zipped file.

On the application server, OPEN DATASET statement may be used with FILTER addition to compress or uncompress

Note: there is also class CL_ABAP_GZIP, but it can't be used to process files, it may just be used to compress or uncompress data in memory.

Working with special files

Excel file types

See Excel file types (CSV, XML, XSLX, OLE)

OLE/DOI for Office native files (Excel, Word, etc.)

When a user is connected to SAP via SAPGUI for Windows, you may use OLE ABAP statements (see ABAP documentation), or Desktop Office Integration framework (it also uses OLE).

It is not possible to use OLE or DOI in a program disconnected from SAPGUI (background job for example).

It is possible to create Office documents using XML files

No labels

5 Comments

Saifur Rahaman

My SIP Project In Tata Steel worked out based on this concept..thnx a lot for keep us update about small but important things

Jan 01, 2010

Unknown User (szpe7do)

Do not use SUBST_GET_FILE_LIST!  In BASIS 701, SAP changes the code to a single line:

raise access_error.

You won't get very far with that.

May 31, 2011

Custodio de Oliveira

To read a file from application server in one shot: CL_RSAN_UT_APPSERV_FILE_READER=>APPSERVER_FILE_READ

To write: CL_RSAN_UT_APPSERV_FILE_WRITER=>APPSERVER_FILE_WRITE

Nov 02, 2011

Former Member

This document and the code snippet section is very helpful. But the FM is given for F4 help for Application Server file is F4_DXFILENAME_4_DYNP . I tried use this FM but it is not working for my case. For this requirement /SAPDMC/LSM_F4_SERVER_FILE FM can be used.

Jan 30, 2015

Former Member

Issue with this article, please do NOT recommend using function module EPS_GET_DIRECTORY_LISTING. It is an SAP projects(not PS) function module which requires Projects authorizations which are related to configuration and NOT related to business processes.

call function 'F4_FILENAME'

exporting

program_name  = sy-repid

dynpro_number = sy-dynnr

field_name    = 'PATH'

importing

file_name     = in_file.

Browse the File path selection in the selection screen

Posted onJun 03, 2008 at 04:18 PM | 4k Views

Follow

RSS Feed

I want to give the user the option to browse his/her desktop and selct a file path and windows name by clicking on a drop down.

How do I do that?

Thank you in advance

Add a Comment

Alert Moderator

Assigned Tags

ABAP Development

Related questions

Regarding Presentation server and Application server path validations

ByFormer Member Jun 11, 2007

want to browse path to the selection-screen..?

ByFormer Member May 05, 2009

5 Answers

Sort by

Votes

|

Newest

|

Oldest

Gajendra Bhatt

Posted onJun 03, 2008 at 04:30 PM

2

Syed,

You can achieve this by calling the function module WS_FILENAME_GET in the the event "AT SELECTION-SCREEN ON VALUE-REQUEST FOR <parameter>". See the code snippet below.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

call function 'WS_FILENAME_GET'

exporting

def_filename = P_FNAME

def_path = '\'

mask = ',.,.csv;.txt.'

mode = 'O'

title = 'Select filename'

importing

filename = P_FNAME

exceptions

inv_winsys = 01

no_batch = 02

selection_cancel = 03

selection_error = 04.

Hope this helps you.

Gajendra

Add a Comment

Alert Moderator

Share

Former Member

Posted onJun 03, 2008 at 04:40 PM

2

Hi,

You can use following code snippet

parameters: p_file type localfile.

at selection-screen on value-request for p_file.

data: ifile type filetable.

data: xfile like line of ifile.

data: rc type i.

call method cl_gui_frontend_services=>file_open_dialog

exporting

initial_directory

= 'C:\'

changing

file_table = ifile

rc = rc.

read table ifile into xfile index 1.

check sy-subrc = 0.

p_file = xfile-filename.

Cheers,

Vikram

Add a Comment

Alert Moderator

Share

Former Member

Posted onJun 03, 2008 at 04:26 PM

0

hi ,

there are so many function modules which will provide F4 Help for the path.

Suppose you have given a parameter name path.

You have to use these FMs in the event

AT SELECTION-SCREEN ON VALUE-REQUEST FOR path.

Example

data : C_FNH_MASK TYPE DXFIELDS-FILEMASK VALUE '.',

SEARCH_DIR TYPE DXFIELDS-LONGPATH VALUE '/SAPGLOBAL/USERS',

FILE_PATH LIKE DXFIELDS-LONGPATH.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR path.

CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'

EXPORTING

I_LOCATION_FLAG = 'P'

I_SERVER = ' '

I_PATH = SEARCH_DIR

FILEMASK = C_FNH_MASK

FILEOPERATION = 'R'

IMPORTING

O_PATH = PATH

EXCEPTIONS

RFC_ERROR = 1

OTHERS = 2.

CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'

EXPORTING

I_LOCATION_FLAG = 'A'

I_SERVER = ' '

I_PATH = SEARCH_DIR

FILEMASK = C_FNH_MASK

FILEOPERATION = 'R'

IMPORTING

...

郑德鼎

关于作者:郑德鼎

企业信息化与 SAP 技术顾问,长期专注 SAP ABAP、FI/CO、MM、SD 等模块的技术分享与实战经验总结。查看更多介绍

来源说明:本文内容由「SAP ABAP Working with files.docx」整理生成,仅用于内部技术分享与学习交流。