Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8604

Re: How to copy a File from One Remote Server directory and putting it to Other Directory

$
0
0

Hi,

 

In a general way, you will need to :

  • read the file from directory 1
  • hold on memory
  • create the file in directory 2 from memory
  • delete the file from directory 1

 

You can use the following template:

 

* <SIGNATURE>---------------------------------------------------------------------------------------+

* | Static Public Method ZCL_UTIL=>COPY_FILES_SERVER

* +-------------------------------------------------------------------------------------------------+

* | [--->] IM_SOURCE_FILE                 TYPE        EPS2FILNAM

* | [--->] IM_SOURCE_DIRECTORY            TYPE        EPSDIRNAM

* | [--->] IM_TARGET_FILE                 TYPE        EPS2FILNAM

* | [--->] IM_TARGET_DIRECTORY            TYPE        EPSDIRNAM

* | [--->] IM_OVERWRITE_MODE              TYPE        EPSOVRWRI (default =SPACE)

* | [<---] EX_FILE_SIZE                   TYPE        EPSFILSIZ

* | [EXC!] OPEN_INPUT_FILE_FAILED

* | [EXC!] OPEN_OUTPUT_FILE_FAILED

* | [EXC!] WRITE_BLOCK_FAILED

* | [EXC!] READ_BLOCK_FAILED

* | [EXC!] CLOSE_OUTPUT_FILE_FAILED

* +--------------------------------------------------------------------------------------</SIGNATURE>

METHOD copy_files_server.

*-----------------------------------------------------------------------

* Copy CL_CTS_LANGUAGE_FILE_IO=>COPY_FILES_LOCAL

*-----------------------------------------------------------------------

* The only change is related with filename lenght, up to 200 now

*-----------------------------------------------------------------------

  DATA: lv_source_file_path   TYPE trfile,

        lv_source_file_size   TYPE epsfilsiz,

        lv_target_file_path   TYPE trfile,

        lv_target_file_size   TYPE epsfilsiz,

        lv_target_file_exists TYPE c.

 

  DATA: lv_overwrite TYPE c.

 

  DATA: lv_eof_reached TYPE c,

        lv_buffer(20480),

        lv_buflen TYPE i.

 

* check existence and file size of the source file

  CALLFUNCTION'EPS_GET_FILE_ATTRIBUTES'

       EXPORTING

            iv_long_file_name     = im_source_file

            dir_name              = im_source_directory

       IMPORTING

            file_size             = lv_source_file_size

       EXCEPTIONS

            read_directory_failed = 1

            read_attributes_failed = 2

            OTHERS                = 3.

  IF sy-subrc <>0.

    RAISE open_input_file_failed.

  ENDIF.

 

 

* check existence and file size of the target file

  CLEAR lv_target_file_exists.

  CALLFUNCTION'EPS_GET_FILE_ATTRIBUTES'

       EXPORTING

            iv_long_file_name     = im_target_file

            dir_name              = im_target_directory

       IMPORTING

            file_size             = lv_target_file_size

       EXCEPTIONS

            read_directory_failed = 1

            read_attributes_failed = 2

            OTHERS                = 3.

 

  CASE sy-subrc.

    WHEN0.

      lv_target_file_exists = 'X'.

    WHEN1.

      RAISE open_output_file_failed.

    WHEN2.

      lv_overwrite = 'X'.

    WHEN3.

      RAISE open_output_file_failed.

  ENDCASE.

 

 

* depending on overwrite mode: copy files

  CASE im_overwrite_mode.

    WHEN space.

      IF lv_target_file_exists = 'X'AND

         lv_target_file_size >0.

        CLEAR lv_overwrite.

      ELSE.

        lv_overwrite = 'X'.

      ENDIF.

    WHEN'S'.

      IF lv_target_file_exists = 'X'AND

         lv_target_file_size = lv_source_file_size.

        CLEAR lv_overwrite.

      ELSE.

        lv_overwrite = 'X'.

      ENDIF.

    WHEN'F'.

      lv_overwrite = 'X'.

    WHENOTHERS.

      CLEAR lv_overwrite.

  ENDCASE.

 

  IF lv_overwrite <>'X'.

    ex_file_size = lv_source_file_size.

    EXIT.

  ENDIF.

 

* build full file path

  CALL'BUILD_DS_SPEC'ID'FILENAME'FIELD im_source_file

                       ID'PATH'     FIELD im_source_directory

                       ID'RESULT'   FIELD lv_source_file_path.

*  IF lv_source_FILE_PATH = SPACE.

*    RAISE BUILD_PATH_FAILED.

*  ENDIF.

 

  CALL'BUILD_DS_SPEC'ID'FILENAME'FIELD im_target_file

                       ID'PATH'     FIELD im_target_directory

                       ID'RESULT'   FIELD lv_target_file_path.

*  IF lv_target_FILE_PATH = SPACE.

*    RAISE BUILD_PATH_FAILED.

*  ENDIF.

 

* source file and target file are the same:

* nothing to do.

  IF lv_source_file_path = lv_target_file_path.

    ex_file_size = lv_source_file_size.

    EXIT.

  ENDIF.

 

* delete old target file

  IF lv_target_file_exists = 'X'.

    DELETEDATASET lv_target_file_path.

  ENDIF.

 

  CALLFUNCTION'SAPGUI_PROGRESS_INDICATOR'

       EXPORTING

            text = lv_target_file_path.

 

 

* open source and target files and start copying

  OPENDATASET lv_source_file_path FORINPUTINBINARYMODE.

  IF sy-subrc NE0.

    RAISE open_input_file_failed.

  ENDIF.

 

  OPENDATASET lv_target_file_path FOROUTPUTINBINARYMODE.

  IF sy-subrc NE0.

    RAISE open_output_file_failed.

  ENDIF.

 

  CLEAR lv_eof_reached.

 

  DO.

    CLEAR lv_buffer.

 

    READDATASET lv_source_file_path

            INTO lv_buffer LENGTH lv_buflen.

 

    IF sy-subrc = 4.

      lv_eof_reached = 'X'.

    ELSEIF sy-subrc >4.

      RAISE read_block_failed.

    ENDIF.

 

    TRANSFER lv_buffer TO lv_target_file_path

      LENGTH lv_buflen.

 

    IF sy-subrc NE0.

      RAISE write_block_failed.

    ENDIF.

 

    IF lv_eof_reached = 'X'.  EXIT.  ENDIF.

  ENDDO.

 

  CLOSEDATASET lv_source_file_path.

  CLOSEDATASET lv_target_file_path.

  IF sy-subrc <>0.

    RAISE close_output_file_failed.

  ENDIF.

 

  ex_file_size = lv_source_file_size.

ENDMETHOD.


Viewing all articles
Browse latest Browse all 8604

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>