Copyright 2024 - BV TallVision IT

The only way to ensure your file is not appended to while you are reading (e.g.) is by letting the operating system (Unix) do its magic, e.g. a rename. Unix statements or commands can be executed with the following C-routine:

CALL 'SYSTEM' 
  ID 'COMMAND' 
  FIELD 'mv /usr/sap/temporary
            /usr/sap/definite'.

If you don't handle things like this with care, serious trouble could be inflicted...

 Taking the above example a little further, a reply from system call can be captured like this: 

DATA:
  lv_command(50) TYPE c,
  lv_line(150)   TYPE c,
  lt_tab         LIKE TABLE OF lv_line.

lv_command = 'ls -lisa'.
CALL 'SYSTEM' ID 'COMMAND' FIELD lv_command
              ID 'TAB'     FIELD lt_tab.
 

The LT_TAB table will hold the result from the ls -l unix command. A small pitfall: when you define your vl_command variable as a TYPE string, the CALL 'SYSTEM' statement will only set SY-SUBRCand not do anything else. Here's a brief summary of what I used this for (Unix statements):

  • mv /usr/something/source /usr/something/errored/source - moves directory source with all it's content to a subdirectory errored
  • rm -rf /usr/something/errored/source remove directory source including all it's content (files and subdirectories)
  • cp /usr/something/source/vacancy.xml /usr/something/source/vacancy_copy.xml copy a file

A better and more secure alternative to this would be the use of function module SXPG_CALL_SYSTEM or SXPG_COMMAND_EXECUTE, which will perform more checks and can thus be regarded as more secure. No reason to stop being careful though !.

Function SXPG_COMMAND_LIST_GET returns a list of allowed commands. Of course, there won't be a command for copying, renaming, moving or deleting files on there, so all you need to do is add one. Transaction SM69 will allow you to do this. For a WindowsNT environment, you could add a line with cmd and parameter /c, which will allow access. Alternatively, add a line with cmd copy or cmd ren (rename).

If the files you need to move are text files (or non binary files such as XML) this setup is one SAP promotes:

OPEN DATASET LV_FNAME FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC = 0.
  OPEN DATASET LV_FNAME1 FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
  IF SY-SUBRC = 0.
    DO.
      READ DATASET LV_FAME INTO LV_LINE.       
      IF SY-SUBRC = 0.
        TRANSFER LV_LINE TO LV_FNAME1.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.
    WRITE 'DATA WRITTEN SUCCESSFULLY'.
    CLOSE DATASET LV_FNAME1.
    CLOSE DATASET LV_FNAME.
    DELETE DATASET LV_FNAME.
  ELSE.
    WRITE 'UNABLE TO OPEN TARGET FILE'.
    CLOSE DATASET LV_FNAME.
  ENDIF. 
ELSE. 
  WRITE 'UNABLE TO OPEN SOURCE FILE'.
ENDIF.

Standard report for OS actions

Report RSBDCOS0 - Execute OS command (Logged in SYSLOG and Trace Files).