Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

FTP

Former Member
0 Likes
1,807

Hello All,

I have a requirement to connect to File servers and read a .csv file. The connection is successfull.

But when i try to read the csv file from the host, it returns the exception command_error.

fname = \ D01\IN\PT\SAMPLE.CSV

CALL FUNCTION 'FTP_SERVER_TO_R3'

EXPORTING

handle = mi_handle

fname = fname

CHARACTER_MODE = 'X'

  • IMPORTING

  • BLOB_LENGTH =

TABLES

  • BLOB =

TEXT = chardata

EXCEPTIONS

TCPIP_ERROR = 1

COMMAND_ERROR = 2

DATA_ERROR = 3

OTHERS = 4

Kindly let me know if i am missing some step here?

Thanks in Advance,

Thirumal

1 ACCEPTED SOLUTION
Read only

former_member194669
Active Contributor
0 Likes
1,683

First explore thru standard testing RSFTP002 , This you can copy, or get a file from SAP to/from FTP and check where is the issue

and also file name is case sensitive

a®

9 REPLIES 9
Read only

Former Member
0 Likes
1,683

Try to use file name like this 'fname = D01/IN/PT/SAMPLE.CSV'

Regard

Jerry

Read only

former_member194669
Active Contributor
0 Likes
1,684

First explore thru standard testing RSFTP002 , This you can copy, or get a file from SAP to/from FTP and check where is the issue

and also file name is case sensitive

a®

Read only

0 Likes
1,683

Hi,

Check this code . It will help you lot.

REPORT ZFTP_CONNECT.

DATA: BEGIN OF MTAB_DATA OCCURS 0,

LINE(132) TYPE C,

END OF MTAB_DATA.

DATA: BEGIN OF MTAB_DATA1 OCCURS 0,

LINE(132) TYPE C,

END OF MTAB_DATA1.

DATA : ERROR TYPE STRING.

DATA: MC_PASSWORD(20) TYPE C,

ERR(20) TYPE C,

MI_KEY TYPE I VALUE 26101957,

MI_PWD_LEN TYPE I,

MI_HANDLE TYPE I.

DATA: MAT TYPE BAPIMATALL.

DATA: LV_USER(30) TYPE C,

LV_PWD(30) TYPE C ,

LV_HOST(64) TYPE C,

IV_FILE_PATH(20) TYPE C,

CMD(120).

DATA: LV_DEST TYPE RFCDES-RFCDEST.

START-OF-SELECTION.

CALL FUNCTION 'FTP_CONNECT'

EXPORTING

*-- Your SAP-UNIX FTP user name (case sensitive)

USER = LV_USER

PASSWORD = LV_PWD

*-- Your SAP-UNIX server host name (case sensitive)

HOST = 'XXX.XX.XX.XX' u2026u2026u2026..(specify machine ip of your FTP )

RFC_DESTINATION = 'SAPFTP'

IMPORTING

HANDLE = MI_HANDLE

EXCEPTIONS

NOT_CONNECTED = 1

OTHERS = 2.

CHECK SY-SUBRC = 0.

CMD = 'cd \Material" "Master'.

*--This is sender directory path

*--Note down that if there is space in directory name it can be handled by u201C u201D

*--command cd \Material master will generate error

PERFORM FTP_COMMAND USING CMD.

CMD = 'ascii'.

*FTP transfers are generally performed in either ASCII or binary mode. Binary mode is * *the preferred mode to transfer non-text files. ASCII mode is the preferred mode to ----transfer text files.

PERFORM FTP_COMMAND USING CMD.

CMD = 'lcd d:\ftp'. u201CThis is target directory path on local machine

PERFORM FTP_COMMAND USING CMD.

CMD = 'ls'.

*-- list all the files available under source folder

PERFORM FTP_COMMAND USING CMD.

*-- MTAB_DATA contains file names of all files under source folder

LOOP AT MTAB_DATA FROM 2.

CONCATENATE 'get' MTAB_DATA+39(80) INTO CMD SEPARATED BY SPACE.

CALL FUNCTION 'FTP_COMMAND'

EXPORTING

HANDLE = MI_HANDLE

COMMAND = CMD

TABLES

DATA = MTAB_DATA1

EXCEPTIONS

TCPIP_ERROR = 1

COMMAND_ERROR = 2

DATA_ERROR = 3

OTHERS = 4

.

IF SY-SUBRC = 0.

LOOP AT MTAB_DATA1.

WRITE: / MTAB_DATA1.

ENDLOOP.

ELSE.

  • do some error checking.

CONCATENATE 'Error in FTP Command while executing' CMD INTO ERROR SEPARATED BY SPACE.

WRITE: / ERROR.

ENDIF.

CLEAR MTAB_DATA1[].

  • concatenate 'delete' MTAB_DATA+39(80) into cmd separated by SPACE.

  • perform ftp_command using cmd.

  • If you want delete file after copy command you can run delete command

ENDLOOP.

CLEAR MTAB_DATA[].

*--Disconnect from FTP server

CALL FUNCTION 'FTP_DISCONNECT'

EXPORTING

HANDLE = MI_HANDLE

EXCEPTIONS

OTHERS = 1.

*--Close RFC connection

CALL FUNCTION 'RFC_CONNECTION_CLOSE'

EXPORTING

DESTINATION = 'SAPFTP'

EXCEPTIONS

OTHERS = 1.

&----


*& Form ftp_command

&----


  • text

----


  • -->P_CMD text

----


FORM FTP_COMMAND USING P_CMD.

CALL FUNCTION 'FTP_COMMAND'

EXPORTING

HANDLE = MI_HANDLE

COMMAND = P_CMD

TABLES

DATA = MTAB_DATA

EXCEPTIONS

TCPIP_ERROR = 1

COMMAND_ERROR = 2

DATA_ERROR = 3

OTHERS = 4.

IF SY-SUBRC = 0.

LOOP AT MTAB_DATA.

WRITE: / MTAB_DATA.

ENDLOOP.

ELSE.

  • do some error checking.

WRITE: / 'Error in FTP Command'.

ENDIF.

IF P_CMD <> 'ls'.

CLEAR MTAB_DATA[].

ENDIF.

ENDFORM. " ftp_command

Read only

former_member188827
Active Contributor
0 Likes
1,683

i think the file path is incorrect.try giving 'PT\SAMPLE.CSV' if the folder Pt is in ftp root directory

Read only

0 Likes
1,683

Dear All,

Thanks for your inputs, my problem is still not solved

i have used the file name is in different format, but it does not work? I am checking how i can read using FTP_Command now.

Thanks,

Thirumal

Edited by: Thirumal R on Jul 22, 2009 7:28 AM

Read only

0 Likes
1,683

Hi Thirumal,

Before using the FM FTP_COMMAND first use the FM FTP_CONNECT to set the connection

the parameters in this FM are

User = 'give the user name (FTP Path User name)

Password = 'Password of FTP server'

HOST = give the host name or Host ip address'

RFC_DESTINATION = 'SAPFTPA'

Then u will use the FM FTP_COMMAND to access the file in the FTP Server

the parameters are

Handle = having Integer value

Command = ' hard code the csv file name ' (Exampe get test.txt)

and get the data to an internal table

Then disconnect this connection by using FM FTP_DISCONNECT .

at last use the FM AUTHORITY_CHECK_DATASET for checking the authorization for the server

then access the data by using Open dataset, Read dataset and Transfer keywords.

Thanks & Regards,

John Victor

Read only

0 Likes
1,683

Hi,

I am using the following commands in program RSFTP002

1. " lcd
sh3pf003\D01 " - to go to that path

2. "get sample.csv" - to read the file

lcd
sh3pf003\D01

Local directory now
sh3pf003\D01

get sample.csv

200 PORT command successful.

150 Opening BINARY mode data connection for sample.csv(0 bytes).

226 Transfer complete.

The problem is after this when i go and see the sample.csv file it is empty(0 bytes), i am not sure where the data goes.

Is there any more command that should be executed to read the data??

Thanks in Advance.

Read only

0 Likes
1,683

Hi Thirumal ,

Kindly specify the location where u will require that file.

Or you can call the FM FTP_COMMAND .

In this function module you will pass the parameter command as : Get <file path on ftp server> <file path where the file need to be put>.

Hope this solves your query.

Regards,

Anuj

Read only

0 Likes
1,683

Hi Anuj,

I want to read that into a internal table.

Thanks,

Thirumal