ABAP顾问
分类:
2007-06-07 18:38:04
REPORT z_barry_itab_to_dataset_ftp .
DATA: BEGIN OF intab OCCURS 0 ,
f1 LIKE mseg-menge,
f2(10) TYPE c,
f3(10) TYPE c,
f4 TYPE c,
END OF intab.
DATA dname(40) TYPE c VALUE '/usr/sap/BQD/DVEBMGS00/work/0001.txt' .
PARAMETERS: suser(30) TYPE c LOWER CASE DEFAULT 'user01',
spwd(30) TYPE c LOWER CASE DEFAULT 'aaaaaa' ,
shost(64) TYPE c LOWER CASE DEFAULT '192.168.242.72' ,
spath(64) TYPE c LOWER CASE DEFAULT '' .
START-OF-SELECTION.
intab-f1 = '-34.32'.
intab-f2 = ''.
intab-f3 = 'a3'.
APPEND intab.
intab-f1 = '3'.
intab-f2 = 'b2'.
intab-f3 = 'b3'.
intab-f4 = 'a'.
APPEND intab.
PERFORM itabtodataset TABLES intab[]
USING dname.
PERFORM toftp.
*---------------------------------------------------------------------*
* FORM ftoline *
*---------------------------------------------------------------------*
FORM itabtodataset TABLES intab
USING dname TYPE c.
DATA: BEGIN OF outtab OCCURS 0,
line(1000) TYPE c,
END OF outtab.
DATA: tab TYPE x VALUE '09',
enter(2) TYPE x VALUE '0D0A'.
DATA: BEGIN OF headtab OCCURS 0 ,
length TYPE i ,
decimals TYPE i,
type_kind TYPE c,
name(30) TYPE c,
END OF headtab.
DATA n TYPE i .
DATA descr_ref TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS:
DATA:str TYPE string ,
text1 TYPE c.
descr_ref ?= cl_abap_typedescr=>describe_by_data( intab ).
LOOP AT descr_ref->components ASSIGNING
MOVE-CORRESPONDING
APPEND headtab.
ENDLOOP.
DESCRIBE TABLE headtab LINES n.
LOOP AT intab ASSIGNING
DO n TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE
str =
READ TABLE headtab INDEX sy-index.
IF headtab-type_kind = 'I' OR headtab-type_kind = 'P'.
SEARCH str FOR '-'.
IF sy-subrc = 0 AND sy-fdpos <> 0.
SPLIT str AT '-' INTO str text1.
CONDENSE str.
CONCATENATE '-' str INTO str.
ELSE.
CONDENSE str.
ENDIF.
ENDIF.
CONCATENATE outtab-line tab str INTO outtab-line.
ENDDO.
SHIFT outtab.
APPEND outtab.
CLEAR outtab.
ENDLOOP.
OPEN DATASET dname FOR OUTPUT IN BINARY MODE .
IF sy-subrc NE 0.
EXIT.
ENDIF.
LOOP AT outtab.
str = outtab.
TRANSFER str TO dname.
TRANSFER enter TO dname.
ENDLOOP.
CLOSE DATASET dname.
ENDFORM.
*---------------------------------------------------------------------*
* FORM toftp *
*---------------------------------------------------------------------*
FORM toftp.
DATA: text1(1) TYPE c ,
commtext(50) .
DATA: BEGIN OF ftp_result OCCURS 0,
line(100),
END OF ftp_result.
DATA: mi_key TYPE i VALUE 26101957,
mi_handle TYPE i,
slen TYPE i .
DATA : filename(128) .
slen = strlen( spwd ).
CALL 'AB_RFC_X_SCRAMBLE_STRING'
ID 'SOURCE' FIELD spwd
ID 'KEY' FIELD mi_key
ID 'SCR' FIELD 'X'
ID 'DESTINATION' FIELD spwd
ID 'DSTLEN' FIELD slen.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = suser
password = spwd
host = shost
rfc_destination = 'SAPFTPBARRY'
IMPORTING
handle = mi_handle
EXCEPTIONS
not_connected = 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.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = mi_handle
command = 'binary'
TABLES
data = ftp_result
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.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = mi_handle
command = 'put 0001.txt'
TABLES
data = ftp_result
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.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = mi_handle
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.