You are on page 1of 13

External Operating System commands in SAP

This article explains how to create a external OS command in SAP through transaction SM69 and execute it using
ABAP.

Here, as an example we will create a OS command for renaming application server files in SAP. Steps below:

Go to transaction SM69, hit on the create button and proceed as shown in the below screen shot:
Now, you can execute the command by hitting the "Execute" button. And below is a code snippet to execute this OS
command from ABAP:

report z_exe_oscmd.

data lt_exec type table of btcxpm.

call function 'SXPG_CALL_SYSTEM'


exporting
commandname = 'ZRENAME'
* ADDITIONAL_PARAMETERS = ' '
* TRACE =
* IMPORTING
* STATUS =
* EXITCODE =
tables
exec_protocol = lt_exec
exceptions
no_permission = 1
command_not_found = 2
parameters_too_long = 3
security_risk = 4
wrong_check_call_interface = 5
program_start_error = 6
program_termination_error = 7
x_error = 8
parameter_expected = 9
too_many_parameters = 10
illegal_command = 11
others = 12
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

As hinted at above, I'd set up a logical command using SM69 (as "copy" would be different in
Windows vs Unix), e.g. Y_FILE_COPY, and then use something like the code below.

data:
l_result(255) type c,
l_status like extcmdexex-status,
l_parm like sxpgcolist-parameters,
lt_prot like btcxpm occurs 10 with header line.

concatenate i_filename i_target_directory into l_parm


separated by space.

call function 'SXPG_COMMAND_EXECUTE'


exporting
commandname = 'Y_FILE_COPY'
additional_parameters = l_parm
trace = ' '
importing
status = l_status
tables
exec_protocol = lt_prot
exceptions
command_not_found = 01
no_permission = 02
parameters_too_long = 03
parameter_expected = 04
program_start_error = 05
program_termination_error = 06
security_risk = 07
too_many_parameters = 08
wrong_check_call_interface = 09
x_error = 10
others = 11.

Move a file in SAP directories(AL11) from


source path to target path
n previous posts i had mentioned about finding all files under a path in SAP directories. After
finding all these files i had a requirement to move them into a permanent location from the temporary
path. ARCHIVFILE_SERVER_TO_SERVER helps achieving copying of file to a desired path from
the source. And after relocation if you do not need file anymore in source path ,you can delete it
by DELETE DATASET with source path.

*&---------------------------------------------------------------------*
*& Report ZYA_MOVE_FILE
*&
*&---------------------------------------------------------------------*
report zya_move_file.

parameters p_path_s like dxfile-filename default '/usr/sap/source/'.


parameters p_path_t like dxfile-filename default '/usr/sap/target/'.
parameters p_file type pfeflname default 'sample.txt'.

data gv_sourcepath like sapb-sappfad.


data gv_targetpath like sapb-sappfad.

concatenate p_path_s p_file into gv_sourcepath .


concatenate p_path_t p_file into gv_targetpath .

* move file from source path to target path


call function 'ARCHIVFILE_SERVER_TO_SERVER'
exporting
sourcepath = gv_sourcepath
targetpath = gv_targetpath
exceptions
error_file =1
no_authorization = 2
others = 3.
if sy-subrc eq 0.
* delete source file
delete dataset gv_sourcepath.
else.
write 'error while moving file'.
endif.

Move a file from server path to local path


and vice versa

In move a file in SAP directories(AL11) from source path to target path post i have tried to explain,
how a file could be transported from one SAP server path to another SAP server path. Lately one of
my friend asked me how to transport from a local path to SAP server path. I directly suggested
CG3Y and CG3Z transactions. But as it was a BW system these transactions were not existing in
their SAP component.

Afterwards we
found ARCHIVFILE_SERVER_TO_CLIENT and ARCHIVFILE_CLIENT_TO_SERVER. These are
exactly doing what CG3Y and CG3Z transactions do. They have source and target paths as inputs
and populating these two fields they are directly doing the move operation.
Various ways for File Moving from one folder to
other on application server

Business Requirement
Move file on application server (T coade AL11) from one folder to another folder

Implementation Logic in BW
This can be achieved in BW in three different ways:

1. Using Trasnfer dataset statement


2. Using external operating system commands
3. Using function module ‘ARCHIVFILE_SERVER_TO_SERVER’

Logic
Case 1: Using Transfer Dataset statement
ABAP Code
PARAMETERS: PS_dir(50) TYPE C ,
pa_dir(50) TYPE c
PF_name(50) TYPE C OBLIGATORY LOWER CASE.
DATA: L_OLDFILE type localfile,
L_NEWFILE type localfile,
L_NEWLINE(240) type c,
Concatenate ps_dir pf_ name into l_oldfile.
Concatenate pa_dir pf_name into l_newfile.
OPEN DATASET l_oldfile FOR INPUT IN BINARY MODE.
If sy-subrc = 0.

OPEN DATASET l_newfile FOR OUTPUT IN BINARY MODE.

If sy-subrc = 0.

DO.
READ DATASET l_oldfile INTO l_newline.
IF sy-subrc EQ 0.
TRANSFER l_newline TO l_newfile.
ELSE.
if l_newline is not initial.
TRANSFER l_newline TO l_newfile.
endif.
EXIT.
ENDIF.
ENDDO.
Endif.

CLOSE DATASET l_newfile.

Else.

Message ‘Can not open source file’ type ‘E’.

Endif.

CLOSE DATASET l_oldfile.

DELETE DATASET l_oldfile.

Advantages of this approach:


Simple Process which transfers data in set of 240 characters

Disadvantages
Data is handled while moving file

For large files program will take more time

Case 2: Using External Operating System Command


ABAP Code
PARAMETERS: p_src TYPE sapb-sappfad,
p_tgt TYPE sapb-sappfad.
DATA: l_para TYPE btcxpgpar,
l_stat(1) TYPE c.
CONCATENATE p_src p_tgt INTO l_para SEPARATED BY space.
CALL FUNCTION ‘SXPG_COMMAND_EXECUTE’
EXPORTING
commandname = ‘ZMV’
additional_parameters = l_para
IMPORTING
status = l_stat
EXCEPTIONS
no_permission = 1
command_not_found = 2
parameters_too_long = 3
security_risk = 4
wrong_check_call_interface = 5
program_start_error = 6
program_termination_error = 7
x_error = 8
parameter_expected = 9
too_many_parameters = 10
illegal_command = 11
wrong_asynchronous_parameters = 12
cant_enq_tbtco_entry = 13
jobcount_generation_error = 14
OTHERS = 15
.IF sy-subrc <> 0.
MESSAGE ‘Error’ TYPE ‘E’ DISPLAY LIKE ‘I’.
ELSE.
if l_stat = ‘O’.
message ‘File Archived successfully’ type ‘I’.
endif.
ENDIF.

Advantages :
o No file data handling
o Time taken is less as we using system command to move file

Disadvantages:
o We need create Operating system command in T code SM49

Case 3: Using Function module


‘ARCHIVFILE_SERVER_TO_SERVER’
ABAP Code:
PARAMETERS: p_src TYPE sapb-sappfad,
p_tgt TYPE sapb-sappfad.

CALL FUNCTION ‘ARCHIVFILE_SERVER_TO_SERVER’


EXPORTING
sourcepath = p_src
targetpath = p_tgt* IMPORTING* LENGTH =
EXCEPTIONS
error_file = 1
no_authorization = 2
OTHERS = 3
.IF sy-subrc <> 0.
WRITE : ‘Error in archival’, sy-subrc.
ELSE.
WRITE ‘File archived successfully’.
ENDIF.

Advantages:
o Standard Function Module is used for file archival

Disadvantages
o File names passed to Function Module should be logical paths to file
o How to create logical path for file is given in below link
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/60e86545-9940-
2d10-9f89-eb5bce1f9228?QuickLink=index&overridelayout=true&47519518718715
Creation of External Commands with the help of
UNIX Coding in SAP
Table of contents

 Introduction
 Create the command in SM49
 Create the ABAP program which calls SXPG_COMMAND_EXECUTE
 Result
 Other links

Introduction
This gives an overview on how to create the External Commands using SM49, assigning UNIX (or
other operating system) file or using a UNIX Command and implementing them. Below given is the
step by step approach to the same.

Example: Listing the Files in the required directory.

Create the command in SM49


Navigate to SM49 and Create an External Command
 In the Command section, the Operating System field is protected but may be changed by pressing
the search help (F4)
 In the Definition section, for Operating System Command text box we can even give the file path of
application server file. All the UNIX coding can be done in the file. So when ever this external
command is called, the functionality written in the file gets triggered. For this example write 'ls'.

Create the ABAP program which calls


SXPG_COMMAND_EXECUTE
Once the Command is created, then use function module 'SXPG_COMMAND_EXECUTE' as shown
below.
Be Careful
The operatingsystem parameter is case sensitive, it must be exactly the same value as in SM69. For
example, you must enter "Linux" instead of "LINUX".

Error rendering macro 'code': Invalid value specified for parameter


'com.atlassian.confluence.ext.code.render.InvalidValueException'
CONSTANTS: c_extcom TYPE sxpgcolist-name VALUE 'Z_LIST_FILES',
c_oper TYPE syopsys VALUE 'UNIX'.

DATA: v_dir_input TYPE sxpgcolist-parameters. " Input Directory


DATA: t_result TYPE STANDARD TABLE OF btcxpm.

v_dir_input = './sap/tmp'.

CALL FUNCTION 'SXPG_COMMAND_EXECUTE'


EXPORTING
commandname = c_extcom
additional_parameters = v_dir_input
operatingsystem = c_oper
TABLES
exec_protocol = t_result
EXCEPTIONS
no_permission = 1
command_not_found = 2
parameters_too_long = 3
security_risk = 4
wrong_check_call_interface = 5
program_start_error = 6
program_termination_error = 7
x_error = 8
parameter_expected = 9
too_many_parameters = 10
illegal_command = 11
wrong_asynchronous_parameters = 12
cant_enq_tbtco_entry = 13
jobcount_generation_error = 14
OTHERS = 15.

Result
T_RESULT will contain the list of files in the specified directory after execution of the function
module.

Example of result:
LENGTH MESSAGE

LENGTH MESSAGE

10 adm_script

7 backups

3 bin

4 boot

4 data

3 dev

7 distrib

3 etc

If the command fails, you would get something like that:


LENGTH MESSAGE

40 ls: ./sap/tmp: No such file or directory

44 External program terminated with exit code 1

This way we can make use of UNIX coding in SAP.

Other links
 SAP Library - Programming Techniques (about SXPG_* function modules)
 Move a file from source to target directories

You might also like