SAPHub

Copy File from One Directory to Another in Application Server

Below program uses COPY_FILES_LOCAL static method of class CL_CTS_LANGUAGE_FILE_IO to copy a file from one folder to another folder in SAP application server.

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: g_s_file TYPE epsfilnam,
      g_s_dir  TYPE epsdirnam,
      g_t_file TYPE epsfilnam,
      g_t_dir  TYPE epsdirnam.
*&---------------------------------------------------------------------*
*& Selection screen
*&---------------------------------------------------------------------*
PARAMETERS: p_s_file TYPE pathextern OBLIGATORY, "Source File
            p_s_dir  TYPE pathextern OBLIGATORY, "Source Directory
            p_t_file TYPE pathextern OBLIGATORY, "Target File
            p_t_dir  TYPE pathextern OBLIGATORY. "Target Directory
*&---------------------------------------------------------------------*
*& Start Of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.

  g_s_file = p_s_file.
  g_s_dir  = p_s_dir.
  g_t_file = p_t_file.
  g_t_dir  = p_t_dir.

*Copy file from Source directory to Target directory
  CALL METHOD cl_cts_language_file_io=>copy_files_local
    EXPORTING
      im_source_file           = g_s_file
      im_source_directory      = g_s_dir
      im_target_file           = g_t_file
      im_target_directory      = g_t_dir
      im_overwrite_mode        = 'F'
    EXCEPTIONS
      open_input_file_failed   = 1
      open_output_file_failed  = 2
      write_block_failed       = 3
      read_block_failed        = 4
      close_output_file_failed = 5
      OTHERS                   = 6.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

The different overwrite options can be found in below table.

Value Description
Don’t overwrite if file exists with size > 0
S Overwrite if file exists with wrong size
F Force overwrite
R Restart mode