0% found this document useful (0 votes)
118 views6 pages

SAP FTP File Transfer Script

This document contains an ABAP program that connects to an FTP server to transfer a file. It defines data types and variables for the FTP connection parameters and file details. It connects to the FTP server, sets the transfer mode to ASCII and passive mode, and transfers a file containing data from a table variable to the FTP server. It then disconnects from the FTP server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views6 pages

SAP FTP File Transfer Script

This document contains an ABAP program that connects to an FTP server to transfer a file. It defines data types and variables for the FTP connection parameters and file details. It connects to the FTP server, sets the transfer mode to ASCII and passive mode, and transfers a file containing data from a table variable to the FTP server. It then disconnects from the FTP server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

FORM GRABA_FTP .

TYPES: BEGIN OF ty_salida,


linea(1000) TYPE c,
END OF ty_salida.
TYPES: BEGIN OF text,
line(120) TYPE c,
END OF text.

DATA: bindata TYPE TABLE OF blob WITH HEADER LINE,


result TYPE TABLE OF text WITH HEADER LINE.

DATA: user(30) TYPE c,


pwd(30) TYPE c,
host(64) TYPE c.

DATA: dest TYPE rfcdes-rfcdest.

DATA: key TYPE i VALUE 26101957,


hdl TYPE i,
slen TYPE i,
docid(100) TYPE c,
bline(120) TYPE x,
cmd(120),
filesize TYPE i,
filedate(10),
filetime(10),
error,
vl_nombre(100) TYPE c.

DATA: lt_data TYPE STANDARD TABLE OF ty_salida,


lv_data(1000) TYPE c,
lv_aux_msg(30),
mat40(40).
data: ruta type ztftpsk-ruta.
data: lv_return type return.
data: lv_objeto type ztftpsk-objeto.

DATA: vl_file TYPE rlgrap-filename value 'FBL5n_'.

CONSTANTS: lc_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.

**********************************************************************
loop at ti_salida.
concatenate ti_salida-sociedad ';'
ti_salida-nombre ';'
ti_salida-rut ';'
ti_salida-cuenta ';'
ti_salida-cod_cliente ';'
ti_salida-clase_doc ';'
ti_salida-num_doc ';'
ti_salida-cme ';'
ti_salida-referencia ';'
ti_salida-factura ';'
ti_salida-fec_doc ';'
ti_salida-venc_neto ';'
ti_salida-demora ';'
ti_salida-sector ';'
ti_salida-importe_ml ';'
ti_salida-moneda_loc ';'
ti_salida-importe2 ';'
ti_salida-moneda2 ';'
ti_salida-asignacion ';'
ti_salida-non_banco ';'
ti_salida-url
into lv_data .
append lv_data to lt_data .

endloop.

move sy-repid to lv_objeto.


CALL FUNCTION 'ZFTP_CONNECT'
EXPORTING
E_OBJETO = lv_objeto
E_SYSID = 'ERP'
E_BATCH = ' '
IMPORTING
S_DESTINO = dest
S_HOST = host
S_USUARIO = user
S_CLAVE = pwd
S_RUTA = ruta
S_RETURN = lv_return .

concatenate 'cd' ruta into cmd separated by space. "ru


ta grabacion FTP

SET EXTENDED CHECK OFF.


error = 0.

slen = STRLEN( pwd ).

CALL FUNCTION 'HTTP_SCRAMBLE'


EXPORTING
SOURCE = pwd
sourcelen = slen
key = key
IMPORTING
destination = pwd.

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'


EXPORTING
text = 'Connect to FTP Server'.

CALL FUNCTION 'FTP_CONNECT'


EXPORTING
user = user
password = pwd
host = host
rfc_destination = dest
IMPORTING
handle = hdl.

IF sy-subrc NE 0.
FORMAT COLOR COL_NEGATIVE.
MOVE host TO lv_aux_msg.
WRITE:/ 'No fue posible conectarse al host ', host.
ELSE.
FORMAT COLOR COL_POSITIVE.
MOVE hdl TO lv_aux_msg.
WRITE:/ 'Conexin Exitosa. Sesin ', lv_aux_msg.

*--nombre del archivo


CONCATENATE vl_file sy-datum '_' sy-uzeit '.txt'
INTO docid.
vl_nombre = docid.

* Mod IM
* Seteo modo pasivo
REFRESH result.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = 'set passive on'
TABLES
data = result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.

IF sy-subrc EQ 0.

ENDIF.

* Seteo modo ASCII


REFRESH result.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = 'ascii'
TABLES
data = result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.

IF sy-subrc EQ 0.

ENDIF.

* transfer data REFRESH result.


CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = cmd
* compress = 'N'
TABLES
data = result
EXCEPTIONS
command_error = 1
tcpip_error = 2.

IF sy-subrc NE 0.
FORMAT COLOR COL_NEGATIVE.
MOVE cmd TO lv_aux_msg.
WRITE: / 'Error al ejecutar comando ', lv_aux_msg .
ELSE.
FORMAT COLOR COL_POSITIVE.
MOVE cmd TO lv_aux_msg.
WRITE: / 'Comando exitoso ', lv_aux_msg.

CALL FUNCTION 'FTP_R3_TO_CLIENT'


EXPORTING
fname = docid
rfc_destination = dest
* BLOB_LENGTH =
character_mode = 'X'
TABLES
* BLOB =
text = lt_data
EXCEPTIONS
command_error = 1
data_error = 2
OTHERS = 3
.
IF sy-subrc <> 0.
FORMAT COLOR COL_NEGATIVE.
WRITE: / 'Error en la transferencia del archivo'.
ELSE.
CONCATENATE 'put' docid INTO cmd SEPARATED BY ' '.
REFRESH result.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = cmd.

REFRESH result.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = cmd
verify = 'X'
IMPORTING
filesize = filesize
filedate = filedate
filetime = filetime
TABLES
data = result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.

ENDIF.

IF sy-subrc NE 0.
FORMAT COLOR COL_NEGATIVE.
WRITE: / 'Error en la transferencia del archivo'.
ELSE.
FORMAT COLOR COL_POSITIVE.
MOVE docid TO lv_aux_msg.
WRITE: / 'Transferencia realizada con xito'.
WRITE: / 'Se genero el archivo ', lv_aux_msg.
ENDIF.

CALL FUNCTION 'FTP_DISCONNECT'


EXPORTING
handle = hdl.

IF sy-subrc NE 0.
WRITE:/ 'No fue posible desconectar del FTP Server'.
ELSE.
WRITE:/ 'Desconectado de FTP Server'.
ENDIF.

ENDIF. "Conectar PATH.

ENDIF."Conectando FTP.

*RFC_CONNECTION_CLOSE:This is used to disconnect the RFC connection betwe


en SAP and other system.

CALL FUNCTION 'RFC_CONNECTION_CLOSE'


EXPORTING
destination = dest
EXCEPTIONS
OTHERS = 1.

ENDFORM. " GRABA_FTP

You might also like