Skip to Content

Basic FTP Routine

Header module:

*&---------------------------------------------------------------------*
*& Report  Z_FTP_FILE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
INCLUDE Z_FTP_FILE_TOP.
INCLUDE z_ftp_file_scr.
INCLUDE z_ftp_file_f01.
************************************************************************
*                    START-OF-SELECTION                                *
************************************************************************
START-OF-SELECTION.
* Find FTP connect values
  PERFORM get_zcfig  USING    p_host
                              p_user
                              p_pwd
                     CHANGING t_cryp_pwd
                              t_user
                              t_host.
  PERFORM decrypt_pwd USING    t_cryp_pwd
                      CHANGING t_dcrp_pwd.
  PERFORM send_ftp    TABLES s_files
                      USING  t_host
                             t_user
                             t_dcrp_pwd
                             p_dir
                             p_ldir
                             p_asc.

The top include:

*&---------------------------------------------------------------------*
*& Include Z_FTP_FILE_TOP                                    Report Z_FTP_FILE
*&
*&---------------------------------------------------------------------*
REPORT   Z_FTP_FILE.
data:  BEGIN OF rec_fname,
         filename like NNLZK-filename,
       END OF rec_fname.
data:
       t_pwd  like zcfig-cfig_str,
       t_user like zcfig-cfig_str,
       t_host LIKE zcfig-cfig_str,
       t_pwd2  LIKE zcfig-cfig_str,
       t_user2 LIKE zcfig-cfig_str,
       t_host2 LIKE zcfig-cfig_str.
data:
       t_cryp_pwd TYPE SLD_B64PWD,
       t_dcrp_pwd type SLD_PASSWORD,
       t_scr_pwd(30) TYPE c,
       t_cryp_pwd2 TYPE sld_b64pwd,
       t_dcrp_pwd2 TYPE sld_password,
       t_scr_pwd2(30) TYPE c.
data:
       t_filename like rlgrap-filename.
CONSTANTS: gc_objname TYPE oj_name VALUE 'RECIPIENT'.

The screen include:

*&---------------------------------------------------------------------*
*&  Include           Z_FTP_FILE_SCR
*&---------------------------------------------------------------------*
TABLES: nnlzk.
PARAMETERS: p_host LIKE zcfig-typ_name,
            p_user LIKE zcfig-typ_name,
            p_pwd  LIKE zcfig-typ_name.
SELECT-OPTIONS:    s_files FOR nnlzk-filename NO INTERVALS.
PARAMETERS: p_dir(80) TYPE c,
            p_ldir(80) TYPE c OBLIGATORY,
            p_delay    type i obligatory,
            p_dist     TYPE so_obj_nam,
            p_asc AS CHECKBOX DEFAULT 'X',
            p_tst as checkbox.

The forms include:

*&---------------------------------------------------------------------*
*&  Include           Z_FTP_FILE_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  GET_ZCFIG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_zcfig  USING     p_p_host
                          p_p_user
                          p_p_pwd
                 CHANGING p_t_cryp_pwd
                          p_t_user
                          p_t_host.
  SELECT SINGLE cfig_str INTO (p_t_host) FROM zcfig
         WHERE obj_name = sy-cprog
           AND typ_name = p_p_host
           AND item_no = '0001'.
  SELECT SINGLE cfig_str INTO (p_t_user) FROM zcfig
         WHERE obj_name = sy-cprog
           AND typ_name = p_p_user
           AND item_no = '0001'.
  SELECT SINGLE cfig_str INTO (t_pwd) FROM zcfig
         WHERE obj_name = sy-cprog
           AND typ_name = p_p_pwd
           AND item_no = '0001'.
  MOVE t_pwd TO p_t_cryp_pwd.
ENDFORM.                    " GET_ZCFIG
*&---------------------------------------------------------------------*
*&      Form  DECRYPT_PWD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM decrypt_pwd  USING    p_t_cryp_pwd
                  CHANGING p_t_dcrp_pwd.
  CALL FUNCTION 'DECODE_SLDPWD_BASE64'
    EXPORTING
      pwdbase64 = p_t_cryp_pwd
    IMPORTING
      password  = p_t_dcrp_pwd.
ENDFORM.                    " DECRYPT_PWD
*&---------------------------------------------------------------------*
*&      Form  SEND_FTP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_S_FILES  text
*----------------------------------------------------------------------*
FORM send_ftp  TABLES   p_s_files STRUCTURE rec_fname
               USING    p_t_host
                        p_t_user
                        p_t_dcrp_pwd
                        p_p_dir
                        p_p_ldir
                        p_p_asc.
  DATA:
        t_error(1) TYPE c,
        l_cmd(80)  TYPE c,
        l_hdl TYPE i,
        l_key TYPE i VALUE 26101957,
        l_dstlen TYPE i.
  DATA: BEGIN OF lt_data OCCURS 0,
          line(100) TYPE c,
        END OF lt_data.
  CONSTANTS:
              c_put(3)  TYPE c VALUE 'put',
              c_lcd(3)  TYPE c VALUE 'lcd',
              c_bls(3)  TYPE c VALUE '!ls',
              c_cd(2)   TYPE c VALUE 'cd'.
  l_dstlen = STRLEN( p_t_dcrp_pwd ).
  CALL FUNCTION 'HTTP_SCRAMBLE'
    EXPORTING
      SOURCE      = p_t_dcrp_pwd
      sourcelen   = l_dstlen
      key         = l_key
    IMPORTING
      destination = t_scr_pwd.
  CLEAR t_error.
  LOOP AT p_s_files.
    CALL FUNCTION 'FTP_CONNECT'
      EXPORTING
        user            = p_t_user
        password        = t_scr_pwd
        host            = p_t_host
        rfc_destination = 'SAPFTPA'
      IMPORTING
        handle          = l_hdl
      EXCEPTIONS
        not_connected   = 1
        OTHERS          = 2.
    IF sy-subrc <> 0.
      WRITE: / 'FTP connection could not be established. ',
               'Pl move the files to Logility manually.'.
      SKIP.
      EXIT.
    ENDIF.
    IF p_p_asc IS INITIAL.
    ELSE.
      CALL FUNCTION 'FTP_COMMAND'
        EXPORTING
          handle        = l_hdl
          command       = 'ascii'
          compress      = 'N'
        TABLES
          data          = lt_data
        EXCEPTIONS
          command_error = 1
          tcpip_error   = 2.
    IF sy-subrc <> 0.
      WRITE: / 'ASCII command failed. ',
               'Pl move the files manually.'.
      SKIP.
      EXIT.
    ENDIF.
    ENDIF.
* Move LOM File
* Change working directory
    IF p_p_dir IS INITIAL.
    ELSE.
      CONCATENATE c_cd p_p_dir INTO l_cmd
                  SEPARATED BY space.
      CALL FUNCTION 'FTP_COMMAND'
        EXPORTING
          handle        = l_hdl
          command       = l_cmd
          compress      = 'N'
        TABLES
          data          = lt_data
        EXCEPTIONS
          command_error = 1
          tcpip_error   = 2.
    IF sy-subrc <> 0.
      WRITE: / 'Change working directory command failed. ',
               'Pl move the files manually.'.
      SKIP.
      EXIT.
    ENDIF.
    ENDIF.
    CONCATENATE c_lcd p_p_ldir INTO l_cmd
                SEPARATED BY space.
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = l_hdl
        command       = l_cmd
        compress      = 'N'
      TABLES
        data          = lt_data
      EXCEPTIONS
        command_error = 1
        tcpip_error   = 2.
    IF sy-subrc <> 0.
      WRITE: / 'Change local directory command failed. ',
               'Pl move the files manually.'.
      SKIP.
      EXIT.
    ENDIF.
    CONCATENATE c_put p_s_files-filename+3 INTO l_cmd
                SEPARATED BY space.
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = l_hdl
        command       = l_cmd
        compress      = 'N'
      TABLES
        data          = lt_data
      EXCEPTIONS
        command_error = 1
        tcpip_error   = 2.
    IF sy-subrc <> 0.
      WRITE: / 'FTP Put command failed. ',
               'Pl move the files manually.'.
      SKIP.
      EXIT.
    ENDIF.
    IF sy-subrc IS INITIAL.
      CALL FUNCTION 'FTP_DISCONNECT'
        EXPORTING
          handle = l_hdl.
    IF sy-subrc <> 0.
      WRITE: / 'Disconnect command failed. ',
               'No further files will be moved,.  ',
               'Pl move the files manually.'.
      SKIP.
      EXIT.
    ENDIF.
   ELSE.
      WRITE: / 'Previous error terminated the process. ',
               'No further files will be moved,.  ',
               'Pl move the files manually.'.
      SKIP.
      EXIT.
    ENDIF.
  ENDLOOP.
  WRITE : /10 'FTP Log:'.
  LOOP AT lt_data.
    WRITE: /10 lt_data-line.
  ENDLOOP.
  SKIP.
ENDFORM.                    " SEND_FTP

Please note I have created a Z config table where the host, the userid, and the password are stored.  I encrypt the password before storing.

Neal

5 Comments
You must be Logged on to comment or reply to a post.