Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Move a file from source to target directories or Delete file. I

Z_ARCH_MV command needs to be created in SM49 transaction for your operating system, to move file (mv command in Unix-like systems).

Selection texts to be created:

P_COPY

Copy

P_DELE

Delete

P_MOVE

Move

P_SFILE

Source File

P_S_AS

Source Application Server

P_S_PC

Source Presentation Server

P_TFILE

Target File

P_T_AS

Target Application Server

P_T_PC

Target Presentation Server

P_XFER

Transfer

Messages used:

Message class

Number

Text

FI

899

&1 &2 &3 &4

Error rendering macro 'code': Invalid value specified for parameter 'lang'
REPORT Z_ARCH_MOVE NO STANDARD PAGE HEADING LINE-SIZE 132.
************************************************************************"
* Move a file from source to target directories or Delete file
*
* This program will not run in a productive client.
*
* Source and target may be either on the Presentation or Application
* servers.
*
* You can only copy if either source or target is on the Presentation
* server.
* You can either copy or move if both source and target are on the
* Application server.
*
* When copying, provide the complete file specification for both
* source and target.
*
* I had trouble getting the OS commands to accept an application server
* filespec containing spaces. So there's a warning if you use one. Be
* aware that the operation may not succeed regardless of the program
* messages.
* Tables
************************************************************************
************************************************************************"
* Types
************************************************************************"
TYPE-POOLS: ABAP.
TYPES : BEGIN OF TY_DATA,
          REC TYPE STRING,
        END OF TY_DATA.
TYPES: TYT_DATA TYPE STANDARD TABLE OF TY_DATA.
************************************************************************
* Classes
************************************************************************"
CLASS CL_GUI_FRONTEND_SERVICES DEFINITION LOAD.
************************************************************************
* Data
************************************************************************"
DATA: W_ANSWER TYPE CHAR1,
      W_RC TYPE I.
DATA: BEGIN OF W_FLAG,
        STOP TYPE FLAG,
      END OF W_FLAG.
DATA: T_DATA TYPE TYT_DATA.
************************************************************************
* Selection screen
************************************************************************"
* Customising - change the first block to reflect your requirements.
* Set the "modif id" to "OUT" to make a field output-only
* or to "HID" to hide it completely.* Data selections
*****************
SELECTION-SCREEN: BEGIN OF BLOCK ZSELECTIONS WITH FRAME.
* Action
SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
* Move or Copy
PARAMETERS: P_XFER RADIOBUTTON GROUP ZG1 DEFAULT 'X'.
* Delete
PARAMETERS: P_DELE RADIOBUTTON GROUP ZG1.
SELECTION-SCREEN: END OF BLOCK B1.
* Source
SELECTION-SCREEN: BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.
* Presentation server
PARAMETERS: P_S_PC RADIOBUTTON GROUP ZG2 DEFAULT 'X'.
* Application server
PARAMETERS: P_S_AS RADIOBUTTON GROUP ZG2.
* File name
PARAMETERS: P_SFILE TYPE LOCALFILE.
SELECTION-SCREEN: END OF BLOCK B2.
* Target (Move or Copy only)
SELECTION-SCREEN: BEGIN OF BLOCK B3 WITH FRAME TITLE TEXT-003.
* Presentation server
PARAMETERS: P_T_PC RADIOBUTTON GROUP ZG3.
* Application server
PARAMETERS: P_T_AS RADIOBUTTON GROUP ZG3 DEFAULT 'X'.
* File name
PARAMETERS: P_TFILE TYPE LOCALFILE.
* Between Application servers
SELECTION-SCREEN: BEGIN OF BLOCK B4 WITH FRAME TITLE TEXT-004.
* Move
PARAMETERS: P_MOVE RADIOBUTTON GROUP ZG4 DEFAULT 'X'.
* Copy
PARAMETERS: P_COPY RADIOBUTTON GROUP ZG4.
SELECTION-SCREEN: END OF BLOCK B4.
SELECTION-SCREEN: END OF BLOCK B3.
SELECTION-SCREEN: END OF BLOCK ZSELECTIONS.
************************************************************************
* Events
************************************************************************"
INITIALIZATION.
  PERFORM NOT_IN_PRODUCTION.
AT SELECTION-SCREEN ON BLOCK ZSELECTIONS.
  PERFORM CHECK_SELECTIONS.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_SFILE.
  PERFORM CHOOSE_FILENAME USING 'P_SFILE'
  'P_S_PC'.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_TFILE.
  PERFORM CHOOSE_FILENAME USING 'P_TFILE'
  'P_T_PC'.
START-OF-SELECTION.
  PERFORM DETERMINE_ACTIONS.
END-OF-SELECTION.
  PERFORM STOP_IF_REQUESTED.
************************************************************************
* Forms
************************************************************************"
************************************************************************
FORM CHECK_SELECTIONS.
************************************************************************"
  DATA: L_TEMPFILE TYPE LOCALFILE.
* Need a source file
  IF P_SFILE IS INITIAL.
    MESSAGE E899(FI) WITH 'Source must be specified'.
  ENDIF.
* Need a target file for Move or Copy
  IF P_XFER EQ ABAP_TRUE
        AND P_TFILE IS INITIAL.
    MESSAGE E899(FI) WITH 'Target must be specified'.
  ENDIF.
* Check for spaces in source or target
  IF P_S_AS EQ ABAP_TRUE.
    L_TEMPFILE = P_SFILE.
    CONDENSE L_TEMPFILE NO-GAPS.
    IF L_TEMPFILE NE P_SFILE.
      MESSAGE W899(FI) WITH 'Spaces in source file name'.
    ENDIF.
  ENDIF.
  IF P_T_AS EQ ABAP_TRUE.
    L_TEMPFILE = P_TFILE.
    CONDENSE L_TEMPFILE NO-GAPS.
    IF L_TEMPFILE NE P_TFILE.
      MESSAGE W899(FI) WITH 'Spaces in target file name'.
    ENDIF.
  ENDIF.
ENDFORM.                    "check_selections
************************************************************************
FORM CHOOSE_FILENAME USING PX_FIELD_FILENAME LIKE DYNPREAD-FIELDNAME
      PX_FIELD_PCFLAG LIKE DYNPREAD-FIELDNAME.
************************************************************************"
  DATA: L_LOCATION LIKE DXFIELDS-LOCATION.
  DATA: LT_DYNP TYPE STANDARD TABLE OF DYNPREAD,
        L_DYNP TYPE DYNPREAD.
  L_DYNP-FIELDNAME = PX_FIELD_PCFLAG.
  APPEND L_DYNP TO LT_DYNP.
  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      DYNAME     = SY-CPROG
      DYNUMB     = SY-DYNNR
    TABLES
      DYNPFIELDS = LT_DYNP
    EXCEPTIONS
      OTHERS     = 11.
  IF SY-SUBRC <> 0.
    MESSAGE E899(FI) WITH 'Error (1) reading selection screen'.
  ENDIF.
  READ TABLE LT_DYNP INDEX 1 INTO L_DYNP.
  IF SY-SUBRC EQ 0.
    IF L_DYNP-FIELDVALUE EQ ABAP_TRUE.
      L_LOCATION = 'P'.
    ELSE.
      L_LOCATION = 'A'.
    ENDIF.
  ELSE.
    MESSAGE E899(FI) WITH 'Error (2) reading selection screen'.
  ENDIF.
  CALL FUNCTION 'F4_DXFILENAME_4_DYNP'
    EXPORTING
      DYNPFIELD_FILENAME = PX_FIELD_FILENAME
      DYNAME             = SY-CPROG
      DYNUMB             = SY-DYNNR
      FILETYPE           = 'P'
      LOCATION           = L_LOCATION
      SERVER             = ' '.
ENDFORM.                    "choose_filename
************************************************************************
FORM CONFIRM_DELETE_FILE.
************************************************************************"
  CALL FUNCTION 'POPUP_TO_CONFIRM'
    EXPORTING
      TEXT_QUESTION         = 'Do you really want to delete this file?'
      DISPLAY_CANCEL_BUTTON = ABAP_FALSE
    IMPORTING
      ANSWER                = W_ANSWER
    EXCEPTIONS
      TEXT_NOT_FOUND        = 1
      OTHERS                = 2.
  IF SY-SUBRC NE 0
  OR W_ANSWER NE 1.
    W_FLAG-STOP = ABAP_TRUE.
    MESSAGE S899(FI) WITH 'Deletion not attempted'.
    STOP.
  ENDIF.
ENDFORM.                    "confirm_delete_file
************************************************************************
FORM COPY_FILE_ON_PRES_SRVR USING PX_SOURCE TYPE LOCALFILE
      PX_TARGET TYPE LOCALFILE.
************************************************************************"
  DATA: L_SFILE TYPE STRING,
  L_TFILE TYPE STRING.
  L_SFILE = PX_SOURCE.
  L_TFILE = PX_TARGET.
  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_COPY
    EXPORTING
      SOURCE      = L_SFILE
      DESTINATION = L_TFILE
      OVERWRITE   = ABAP_TRUE
    EXCEPTIONS
      OTHERS      = 13.
  IF SY-SUBRC EQ 0.
    FORMAT COLOR COL_POSITIVE.
    WRITE: / 'Successfully copied file', PX_SOURCE,
          / 'to', PX_TARGET UNDER PX_SOURCE.
  ELSE.
    W_FLAG-STOP = ABAP_TRUE.
    MESSAGE S899(FI)
          WITH 'Failed to copy file' PX_SOURCE
          DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.                    "copy_file_on_pres_srvr
************************************************************************
FORM DELETE_FILE.
************************************************************************"
  DATA: L_FILE TYPE STRING.
  IF P_S_PC EQ ABAP_TRUE.
* Delete Presentation server file
    L_FILE = P_SFILE.
    CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_DELETE
      EXPORTING
        FILENAME             = L_FILE
      CHANGING
        RC                   = W_RC
      EXCEPTIONS
        FILE_DELETE_FAILED   = 1
        CNTL_ERROR           = 2
        ERROR_NO_GUI         = 3
        FILE_NOT_FOUND       = 4
        ACCESS_DENIED        = 5
        UNKNOWN_ERROR        = 6
        NOT_SUPPORTED_BY_GUI = 7
        WRONG_PARAMETER      = 8
        OTHERS               = 9.
  ELSE.
* Delete Application server file
    CATCH SYSTEM-EXCEPTIONS FILE_ACCESS_ERRORS = 4.
      DELETE DATASET P_SFILE.
    ENDCATCH.
  ENDIF.
  IF SY-SUBRC EQ 0.
    FORMAT COLOR COL_POSITIVE.
    WRITE: / 'Successfully deleted file', P_SFILE.
  ELSE.
    W_FLAG-STOP = ABAP_TRUE.
    MESSAGE S899(FI)
          WITH 'Failed to delete file' P_SFILE
          DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.                    "delete_file
************************************************************************
FORM DETERMINE_ACTIONS.
************************************************************************"
  IF P_XFER EQ ABAP_TRUE.
    PERFORM MOVE_OR_COPY.
  ELSE.
    PERFORM CONFIRM_DELETE_FILE.
    PERFORM DELETE_FILE.
  ENDIF.
ENDFORM.                    "determine_actions
************************************************************************
FORM MOVE_FILE_ON_APP_SRVR USING PX_SOURCE TYPE LOCALFILE
      PX_TARGET TYPE LOCALFILE.
************************************************************************"
  DATA: L_EXITCODE TYPE BTCXPGEXIT,
        L_PARMS TYPE BTCXPGPAR,
        L_STATUS TYPE BTCXPGSTAT.
  DATA: LT_PROTOCOL TYPE STANDARD TABLE OF BTCXPM,
        L_PROTOCOL TYPE BTCXPM.
  CONCATENATE PX_SOURCE
        PX_TARGET
        INTO L_PARMS
        SEPARATED BY SPACE.
* External command:
* - Unix - mv
* - Windows - cmd /c move
  CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
    EXPORTING
      COMMANDNAME                   = 'Z_ARCH_MV'
      ADDITIONAL_PARAMETERS         = L_PARMS
    IMPORTING
      STATUS                        = L_STATUS
      EXITCODE                      = L_EXITCODE
    TABLES
      EXEC_PROTOCOL                 = LT_PROTOCOL
    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 NE 0
        OR L_STATUS NE 'O'
        OR L_EXITCODE NE '0'.
* Failure
    FORMAT COLOR COL_NEGATIVE.
    WRITE: / 'Failed to move' , PX_SOURCE,
          / 'to', PX_TARGET UNDER PX_SOURCE.
    FORMAT COLOR COL_NORMAL.
    LOOP AT LT_PROTOCOL INTO L_PROTOCOL.
      WRITE: / L_PROTOCOL-MESSAGE+0(L_PROTOCOL-LENGTH).
    ENDLOOP.
  ELSE.
* Success
    FORMAT COLOR COL_POSITIVE.
    WRITE: / 'Successfully moved' , PX_SOURCE,
          / 'to', PX_TARGET UNDER PX_SOURCE.
  ENDIF.
ENDFORM.                    "move_file_on_app_srvr
************************************************************************
FORM MOVE_OR_COPY.
************************************************************************"
  IF P_S_AS EQ ABAP_TRUE
        AND P_T_AS EQ ABAP_TRUE.
* Source and Target both on AppSrvr
    IF P_MOVE EQ ABAP_TRUE.
      PERFORM MOVE_FILE_ON_APP_SRVR USING P_SFILE
            P_TFILE.
    ELSE.
      PERFORM READ_APPSRVR_FILE USING P_SFILE
            CHANGING T_DATA.
      PERFORM WRITE_APPSRVR_FILE USING P_TFILE
            T_DATA.
    ENDIF.
  ELSEIF P_S_PC EQ ABAP_TRUE
  AND P_T_PC EQ ABAP_TRUE.
* Source and Target both on PresSrvr
    PERFORM COPY_FILE_ON_PRES_SRVR USING P_SFILE
          P_TFILE.
  ELSEIF P_S_PC EQ ABAP_TRUE.
* Source on PresSrvr; Target on AppSrvr
    PERFORM READ_LOCAL_FILE USING P_SFILE
          CHANGING T_DATA.
    PERFORM WRITE_APPSRVR_FILE USING P_TFILE
          T_DATA.
  ELSE.
* Source on AppSrvr; Target on PresSrvr
    PERFORM READ_APPSRVR_FILE USING P_SFILE
          CHANGING T_DATA.
    PERFORM WRITE_LOCAL_FILE USING P_TFILE
          T_DATA.
  ENDIF.
ENDFORM.                    "move_or_copy
************************************************************************
FORM NOT_IN_PRODUCTION.
************************************************************************"
  DATA: L_CCCATEGORY TYPE CCCATEGORY.
  SELECT SINGLE CCCATEGORY INTO L_CCCATEGORY
        FROM T000
        WHERE MANDT EQ SY-MANDT.
  IF L_CCCATEGORY EQ 'P'.
    MESSAGE E899(FI)
          WITH 'Program' SY-REPID
          'is not available in Production clients'.
  ENDIF.
ENDFORM.                    "not_in_production
************************************************************************
FORM READ_APPSRVR_FILE USING PX_SOURCE TYPE LOCALFILE
CHANGING PXT_DATA TYPE TYT_DATA.
************************************************************************"
  DATA: L_DATA TYPE TY_DATA.
  CATCH SYSTEM-EXCEPTIONS FILE_ACCESS_ERRORS = 4.
    OPEN DATASET PX_SOURCE FOR INPUT IN TEXT MODE ENCODING DEFAULT.
    DO.
      READ DATASET PX_SOURCE INTO L_DATA-REC.
      IF SY-SUBRC NE 0.
        EXIT.
      ENDIF.
      APPEND L_DATA TO PXT_DATA.
    ENDDO.
    CLOSE DATASET PX_SOURCE.
  ENDCATCH.
  IF SY-SUBRC EQ 0.
    FORMAT COLOR COL_POSITIVE.
    WRITE: / 'Successfully read', PX_SOURCE.
    FORMAT COLOR COL_NORMAL.
  ELSE.
    W_FLAG-STOP = ABAP_TRUE.
    MESSAGE S899(FI)
          WITH 'Unable to read AppSrvr file' PX_SOURCE
          DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.                    "read_appsrvr_file
************************************************************************
FORM READ_LOCAL_FILE USING PX_SOURCE TYPE LOCALFILE
CHANGING PXT_DATA TYPE TYT_DATA.
************************************************************************
  DATA: L_FILE TYPE STRING.
  L_FILE = PX_SOURCE.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      FILENAME = L_FILE
    TABLES
      DATA_TAB = PXT_DATA
    EXCEPTIONS
      OTHERS   = 17.
  IF SY-SUBRC EQ 0.
    FORMAT COLOR COL_POSITIVE.
    WRITE: / 'Successfully read', PX_SOURCE.
    FORMAT COLOR COL_NORMAL.
  ELSE.
    W_FLAG-STOP = ABAP_TRUE.
    MESSAGE S899(FI)
          WITH 'Unable to read local file' PX_SOURCE
          DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.                    "read_local_file
************************************************************************
FORM STOP_IF_REQUESTED.
************************************************************************
  IF W_FLAG-STOP EQ ABAP_TRUE.
    STOP.
  ENDIF.
ENDFORM.                    "stop_if_requested
************************************************************************
FORM WRITE_APPSRVR_FILE USING PX_TARGET TYPE LOCALFILE
PXT_DATA TYPE TYT_DATA.
************************************************************************
  DATA: L_DATA TYPE TY_DATA.
  CATCH SYSTEM-EXCEPTIONS FILE_ACCESS_ERRORS = 4.
    OPEN DATASET PX_TARGET FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
    LOOP AT PXT_DATA INTO L_DATA.
      TRANSFER L_DATA-REC TO PX_TARGET.
    ENDLOOP.
    CLOSE DATASET PX_TARGET.
  ENDCATCH.
  IF SY-SUBRC EQ 0.
    FORMAT COLOR COL_POSITIVE.
    WRITE: / 'Successfully written', PX_TARGET.
    FORMAT COLOR COL_NORMAL.
  ELSE.
    W_FLAG-STOP = ABAP_TRUE.
    MESSAGE S899(FI)
          WITH 'Unable to write AppSrvr file' PX_TARGET
          DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.                    "write_appsrvr_file
************************************************************************
FORM WRITE_LOCAL_FILE USING PX_TARGET TYPE LOCALFILE
      PXT_DATA TYPE TYT_DATA.
************************************************************************"
  DATA: L_FILE TYPE STRING.
  L_FILE = PX_TARGET.
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      FILENAME = L_FILE
    TABLES
      DATA_TAB = PXT_DATA
    EXCEPTIONS
      OTHERS   = 17.
  IF SY-SUBRC EQ 0.
    FORMAT COLOR COL_POSITIVE.
    WRITE: / 'Successfully written', PX_TARGET.
    FORMAT COLOR COL_NORMAL.
  ELSE.
    W_FLAG-STOP = ABAP_TRUE.
    MESSAGE S899(FI)
          WITH 'Unable to write local file' PX_TARGET
          DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
ENDFORM.                    "write_local_file

1 Comment

  1. Guest

    Does anyone know a method to get a canonical path, so it can be automatically checked if source and destination path is on the same logical drive? This could be used for determining whether to copy/delete or move the file.