메뉴 건너뛰기

SAP 한국 커뮤니티

FTP File Tranfer 샘플 소스

양키(이경환) 2014.03.04 13:11 조회 수 : 5027

REPORT zftptrasfr
NO STANDARD PAGE HEADING LINE-SIZE 255.
 
************************************************************************
*T Y P E S
************************************************************************
TYPES:
BEGIN OF x_cmdout,
  line(100) TYPE c,
END OF x_cmdout.
 
TYPE-POOLS : slis.
 
************************************************************************
*    D A T A
************************************************************************
*data specifications of FTP server
 
*Handler and Key
DATA: w_cmd(40) TYPE c,
      w_hdl TYPE i,
      w_logid TYPE zda_log_ipl VALUE 1,
      w_key TYPE i VALUE 26101957,
      w_slen TYPE i,
      wa_iplftp TYPE ztbl_ipl_ftp,
      it_iplftp TYPE STANDARD TABLE OF ztbl_ipl_ftp,
      wa_cmdout TYPE x_cmdout,
      it_cmdout TYPE STANDARD TABLE OF x_cmdout.
 
*Constant declarations
CONSTANTS:  c_dest TYPE rfcdes-rfcdest VALUE 'SAPFTPA',
            c_host(11) TYPE c VALUE '172.XX.X.XX',
            c_ftp(6) TYPE c VALUE 'FTPDIRECTORY',
            c_sap(16) TYPE c VALUE '/sap/inbound/SAPDIRECTORY'.
 
************************************************************************
*    P A R A M E T E R S
************************************************************************
 
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_user(30) TYPE c LOWER CASE ,
            p_pwd(30) TYPE c LOWER CASE ,
            p_host(64) TYPE c LOWER CASE DEFAULT c_host ,
            p_edi TYPE zedi, " 5-character domain containing EDI profiles
            p_ftp TYPE e_dexcommfilepath LOWER CASE DEFAULT c_ftp,
            p_sap TYPE esefilepath LOWER CASE DEFAULT c_sap.
SELECTION-SCREEN END OF BLOCK b1.
 
************************************************************************
*AT SELECTION SCREEN Events
************************************************************************
 
AT SELECTION-SCREEN OUTPUT.
 
  LOOP AT SCREEN.
    CASE screen-name.
      WHEN 'P_PWD'."Set the password field as invisible
        screen-invisible = '1'.
        MODIFY SCREEN.
    ENDCASE.
  ENDLOOP.
 
************************************************************************
*    S T A R T - O F - S E L E C T I O N
************************************************************************
 
START-OF-SELECTION.
 
*Connect to the FTP server.
  PERFORM ftp_connect.
 
*Find all files in the directory and store inside the Log table
  PERFORM log_files.
 
*Check for transfer of files to FTP.
  IF it_iplftp[] IS INITIAL.
    MESSAGE text-003 TYPE 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.
 
*Change the local save directory and download to SAP application server.
  PERFORM move_files.
 
*Close the connection
  PERFORM close_ftp.
 
************************************************************************
*END-OF-SELECTION
************************************************************************
END-OF-SELECTION.
 
*Display report.
  PERFORM disp_res.
 
*&---------------------------------------------------------------------
*& Form FTP_CONNECT
*&---------------------------------------------------------------------
*Make a connection to the FTP server
*----------------------------------------------------------------------
FORM ftp_connect .
 
  SET EXTENDED CHECK OFF.
  w_slen = strlen( p_pwd ).
 
  CALL FUNCTION 'HTTP_SCRAMBLE'
    EXPORTING
      source      = p_pwd
      sourcelen   = w_slen
      key         = w_key
    IMPORTING
      destination = p_pwd.
 
  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user            = p_user
      password        = p_pwd
      host            = p_host
      rfc_destination = c_dest
    IMPORTING
      handle          = w_hdl
    EXCEPTIONS
      not_connected   = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
*Message will arise in case of any issues in connecting to the FTP server.
    MESSAGE text-e01 TYPE 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.
 
ENDFORM. " FTP_CONNECT
*&---------------------------------------------------------------------
*& Form LOG_FILES
*&---------------------------------------------------------------------
*find all the files in the home directory
*----------------------------------------------------------------------
FORM log_files .
 
*Change directory to the FTP directory for LUPIN
  CONCATENATE 'cd' p_ftp INTO w_cmd SEPARATED BY space.
 
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = w_hdl
      command       = w_cmd
      compress      = 'N'
    TABLES
      data          = it_cmdout
    EXCEPTIONS
      tcpip_error   = 1
      command_error = 2
      data_error    = 3
      OTHERS        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  REFRESH it_cmdout.
  CLEAR w_cmd.
  w_cmd = 'ls'.
 
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = w_hdl
      command       = w_cmd
      compress      = 'N'
    TABLES
      data          = it_cmdout
    EXCEPTIONS
      tcpip_error   = 1
      command_error = 2
      data_error    = 3
      OTHERS        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
*All customized checks for file to be placed here.
  LOOP AT it_cmdout INTO wa_cmdout FROM 4.
 
*Check the command line for a file
    CHECK wa_cmdout-line+68(4) EQ '.txt'.
 
*Move all the files to the FTP table
    wa_iplftp-file_nm = wa_cmdout-line+39(33).
    wa_iplftp-log_id = w_logid.
 
*Currently the transfer date/time will be taken
    wa_iplftp-fdate = sy-datum."WA_CMDOUT-LINE+39(8).
    wa_iplftp-ftime = sy-uzeit."WA_CMDOUT-LINE+48(6).
    wa_iplftp-ernam = sy-uname.
 
    APPEND wa_iplftp TO it_iplftp.
 
  ENDLOOP.
  REFRESH it_cmdout.
 
ENDFORM. " LOG_FILES
*&---------------------------------------------------------------------
*& Form MOVE_FILES
*&---------------------------------------------------------------------
FORM move_files .
 
  DATA : l_indx TYPE i.
 
  CLEAR w_cmd.
  CONCATENATE 'lcd' p_sap INTO w_cmd SEPARATED BY space.
 
*Change the local directory to the sap inbound directory
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = w_hdl
      command       = w_cmd
      compress      = 'N'
    TABLES
      data          = it_cmdout
    EXCEPTIONS
      command_error = 1
      tcpip_error   = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
  REFRESH it_cmdout.
*Move each file from FTP to SAP inbound directory
  LOOP AT it_iplftp INTO wa_iplftp WHERE stat EQ space OR
  stat EQ 'CR'.
 
    l_indx = sy-tabix.
 
    CLEAR w_cmd.
    REFRESH it_cmdout.
 
    CONCATENATE 'get' wa_iplftp-file_nm INTO w_cmd SEPARATED BY space.
 
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = w_hdl
        command       = w_cmd
        compress      = 'N'
      TABLES
        data          = it_cmdout
      EXCEPTIONS
        command_error = 1
        tcpip_error   = 2.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
 
    READ TABLE it_cmdout INTO wa_cmdout INDEX 2.
    IF sy-subrc EQ 0.
      IF wa_cmdout-line CS 'command successful'.
        wa_iplftp-stat = 'CR'.
        wa_iplftp-mess = text-004.
      ELSE.
        wa_iplftp-stat = 'EF'.
        wa_iplftp-mess = text-005.
      ENDIF.
    ENDIF.
 
    MODIFY it_iplftp FROM wa_iplftp INDEX l_indx.
*delete from FTP
    CLEAR w_cmd.
    CONCATENATE 'delete' wa_iplftp-file_nm INTO w_cmd SEPARATED BY space.
 
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = w_hdl
        command       = w_cmd
        compress      = 'N'
      TABLES
        data          = it_cmdout
      EXCEPTIONS
        command_error = 1
        tcpip_error   = 2.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ENDLOOP.
 
*Insert all table entries.
  INSERT ztbl_ipl_ftp FROM TABLE it_iplftp.
  IF sy-subrc NE 0.
    MESSAGE text-006 TYPE 'I'.
  ENDIF.
 
ENDFORM. " MOVE_FILES
*&---------------------------------------------------------------------
*& Form CLOSE_FTP
*&---------------------------------------------------------------------
FORM close_ftp .
 
  CALL FUNCTION 'FTP_DISCONNECT'
    EXPORTING
      handle = w_hdl.
 
  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination = c_dest
    EXCEPTIONS
      OTHERS      = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
ENDFORM. " CLOSE_FTP
*&---------------------------------------------------------------------
*& Form DISP_RES
*&---------------------------------------------------------------------
*Display the output
*----------------------------------------------------------------------
FORM disp_res .
 
  DATA : l_layout TYPE slis_layout_alv.
 
  l_layout-colwidth_optimize = 'X'.
 
*Display table contents of updated files
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      i_structure_name   = 'ZTBL_IPL_FTP'
      is_layout          = l_layout
    TABLES
      t_outtab           = it_iplftp
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
ENDFORM. " DISP_RES
번호 제목 글쓴이 날짜 조회 수
307 SCN에 올라온 ADT Tutorials Link 입니다. [4] Wise 멘토 2012.10.12 4904
306 STABLE SORT [3] sapjoy 2009.03.03 4909
305 ALV autorefresh 입니다. [13] eclipse 2008.01.15 4943
304 MM 이동유형 분류표 적어봅니다. [14] file 양키 2009.08.14 4951
303 ALV 색상 번호와 사용 목적 꿀단지 2011.10.11 4960
302 새로운 Session을 생성하는 방법 [7] 양키 2013.07.17 4962
301 BAPI LIST 입니다. 참고하세요. [24] file 노름마치 2007.12.10 4965
300 월 계산 함수 [5] 오향 2008.02.28 4975
299 <img src=d.gif>[For Power User] Dynamic Creation of Data Objects[추천:e-abap] [3] 양키 2011.01.20 5005
298 ICON ascii code [3] file sapjoy 2007.10.05 5006
297 abap 메모리 구조 [1] SARA 2007.03.16 5026
» FTP File Tranfer 샘플 소스 [1] 양키(이경환) 2014.03.04 5027
295 로그온 후 exit, logon, login [3] sapjoy 2009.12.11 5030
294 workingday 기준으로 N일 후 날짜구하는 펑션입니다. [4] 홍성현 2007.08.08 5032
293 <b>[완료]</b>New abap editor block selection 기능 아시는분... [11] file 숨어푸 2008.08.01 5039
292 Mac Notebook을 쓰는 사람들을 위한 조그만 팁. [2] Bigbrother 2011.09.21 5040
291 여러 DATE 변환 TIP [9] file 푸른창공 2008.09.11 5061
290 OPEN SQL HINT 사용 sapjoy 2007.05.22 5068
289 팝업창이나 서브스크린 디버깅을 쉽게 할 수 있어요 [17] 김창훈 2007.08.08 5072
288 인터널 테이블 또는 테이블 구조 확인 [2] sapjoy 2008.12.29 5080