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

Get file from Server to internal table using FTP

Former Member
0 Likes
5,421

Hi guys,

I dont know how to get or read a file from a server to an internal table in SAP.

I'm able to list what the server has, but how do i get into the file, read the context, get the info...

what i have until now is sth like this


DATA: mc_password(20) TYPE c,
      mi_key TYPE i VALUE 26101957,
      mi_pwd_len TYPE i,
      mi_handle TYPE i,
      cmd(120).

DATA: BEGIN OF mtab_data OCCURS 0,
line(132) TYPE c,
END OF mtab_data.

DATA: wa_data LIKE mtab_data.

CONSTANTS: c_key TYPE i VALUE 26101957.

DATA: g_dpwd(30). "For storing password
CALL FUNCTION 'HTTP_SCRAMBLE'
  EXPORTING
    SOURCE      = mc_password "Actual password
    sourcelen   = '9'
    key         = c_key
  IMPORTING
    destination = mc_password. "Encyrpted Password

CALL FUNCTION 'FTP_CONNECT'
  EXPORTING
    user            = 'srv'
    password        = mc_password
    host            = '200.18.38.954'
    rfc_destination = 'SAPFTP'
  IMPORTING
    handle          = mi_handle
  EXCEPTIONS
    not_connected   = 1
    OTHERS          = 2.

CHECK sy-subrc = 0.

CALL FUNCTION 'FTP_COMMAND'
  EXPORTING
    handle        = mi_handle
    command       = 'dir'
  TABLES
    data          = mtab_data
  EXCEPTIONS
    tcpip_error   = 1
    command_error = 2
    data_error    = 3
    OTHERS        = 4.
* mtab_data contains the list of the all the files that are in the server
*I just want to get an specific file by date, such as 03-01
LOOP AT mtab_data INTO wa_data WHERE LINE CS '03-01'.
* the programs get inside the loop, because it knows that there is a file on 03-01
ENDLOOP.

CALL FUNCTION 'FTP_DISCONNECT'
  EXPORTING
    handle = mi_handle
  EXCEPTIONS
    OTHERS = 1.

what is on mtab_data is sth like this.


1	dir
2	200 PORT command successful.
3	150 Opening BINARY mode data connection for /bin/ls.
4	02-25-11  09:21AM               873288 MM_Costos_20110223.ci
5	03-01-11  08:02AM                   54 MM_Costos_20110301.txt
6	02-23-11  10:12AM               925900 MM_Demanda_20110223.ci
7	03-01-11  08:02AM                 3337 MM_Demanda_20110301.txt
8	02-25-11  09:21AM              1079589 MM_Materiales_20110223.ci
9	03-01-11  08:02AM                  101 MM_Materiales_20110301.txt

I want to get the info of file "5 03-01-11 08:02AM 54 MM_Costos_20110301.txt"

how can i access to that file?

I want to transfer the info of that file into an internal table.

i got it until there, some helps please.

Regards

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,477

look at your OPEN DATASET. . ., READ....., CLOSE keywords using ABAP Help (F1).

Hi guys,

I dont know how to get or read a file from a server to an internal table in SAP.

I'm able to list what the server has, but how do i get into the file, read the context, get the info...

what i have until now is sth like this


DATA: mc_password(20) TYPE c,
      mi_key TYPE i VALUE 26101957,
      mi_pwd_len TYPE i,
      mi_handle TYPE i,
      cmd(120).

DATA: BEGIN OF mtab_data OCCURS 0,
line(132) TYPE c,
END OF mtab_data.

DATA: wa_data LIKE mtab_data.

CONSTANTS: c_key TYPE i VALUE 26101957.

DATA: g_dpwd(30). "For storing password
CALL FUNCTION 'HTTP_SCRAMBLE'
  EXPORTING
    SOURCE      = mc_password "Actual password
    sourcelen   = '9'
    key         = c_key
  IMPORTING
    destination = mc_password. "Encyrpted Password

CALL FUNCTION 'FTP_CONNECT'
  EXPORTING
    user            = 'srv'
    password        = mc_password
    host            = '200.18.38.954'
    rfc_destination = 'SAPFTP'
  IMPORTING
    handle          = mi_handle
  EXCEPTIONS
    not_connected   = 1
    OTHERS          = 2.

CHECK sy-subrc = 0.

CALL FUNCTION 'FTP_COMMAND'
  EXPORTING
    handle        = mi_handle
    command       = 'dir'
  TABLES
    data          = mtab_data
  EXCEPTIONS
    tcpip_error   = 1
    command_error = 2
    data_error    = 3
    OTHERS        = 4.
* mtab_data contains the list of the all the files that are in the server
*I just want to get an specific file by date, such as 03-01
LOOP AT mtab_data INTO wa_data WHERE LINE CS '03-01'.
* the programs get inside the loop, because it knows that there is a file on 03-01
ENDLOOP.

CALL FUNCTION 'FTP_DISCONNECT'
  EXPORTING
    handle = mi_handle
  EXCEPTIONS
    OTHERS = 1.

what is on mtab_data is sth like this.


1	dir
2	200 PORT command successful.
3	150 Opening BINARY mode data connection for /bin/ls.
4	02-25-11  09:21AM               873288 MM_Costos_20110223.ci
5	03-01-11  08:02AM                   54 MM_Costos_20110301.txt
6	02-23-11  10:12AM               925900 MM_Demanda_20110223.ci
7	03-01-11  08:02AM                 3337 MM_Demanda_20110301.txt
8	02-25-11  09:21AM              1079589 MM_Materiales_20110223.ci
9	03-01-11  08:02AM                  101 MM_Materiales_20110301.txt

I want to get the info of file "5 03-01-11 08:02AM 54 MM_Costos_20110301.txt"

how can i access to that file?

I want to transfer the info of that file into an internal table.

i got it until there, some helps please.

Regards

9 REPLIES 9
Read only

Former Member
0 Likes
2,478

look at your OPEN DATASET. . ., READ....., CLOSE keywords using ABAP Help (F1).

Read only

0 Likes
2,477

Hi, thanks for reply.

I did this


  open dataset 'MM_OC_PEND_20110219.txt' for input IN TEXT MODE ENCODING DEFAULT.
WHILE sy-subrc = 0.
  READ DATASET 'MM_OC_PEND_20110219.txt' INTO result.
  WRITE / result.
ENDWHILE.

CLOSE DATASET 'MM_OC_PEND_20110219.txt'.

but sy-subrc = 8 after the open. It means that it didnt open it.

What am i doing wrong?

I thought that i can use some FM to transfer the file that i need to an internal table.

Thanks.

Read only

Former Member
0 Likes
2,477

please go through this link

http://wiki.sdn.sap.com/wiki/display/Snippets/ABAPsolutiontoimplementFTP+transactions

Cheers

S.Janagar

Read only

0 Likes
2,477

Thanks, I checked before, but it wasn't helpful.

I can't open or read the file data.

, I know that I'm missing sth, but until now I'm not able to figurate out what it is.

Thanks,

Read only

0 Likes
2,477

Hi Miguel,

I can't see file 'MM_OC_PEND_20110219.txt' ikn the mtab_data list. Check the offset where the filename starts, take it from there.

Additionally, you need to do ftp get command for fetching the file from ftp server and tansfer it to a directory where you have read authority granted.

Do some more search for ftp. ftp means file transfer protocol: You already accessed the ftp server and asked about the current directory content. Now you may change your local directory, use binary mode to get the file transfered, then finally you can use open dataset.

Regards,

Clemens

Read only

0 Likes
2,477

Hi, yeap, u r right, but i changed the server, before i was poiting to PRD and now i'm using developer server, so that file is on that table.

I tried to so sth like u said to me


LOOP AT mtab_data INTO wa_data WHERE LINE CS '02-19'.

  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = mi_handle
      command       = 'get MM_OC_PEND_20110219.txt'
    TABLES
      data          = mtab_data
    EXCEPTIONS
      tcpip_error   = 1
      command_error = 2
      data_error    = 3
      OTHERS        = 4.

ENDLOOP.

but i got sth like this on the mtab_data


433	226 Transfer complete.
434	get MM_OC_PEND_20110219.txt
435	200 PORT command successful.
436	150 Opening BINARY mode data connection for MM_OC_PEND_20110219.txt(78 bytes).
437	226 Transfer complete.
438	78 bytes received in 0.00 seconds (0.08 Kbytes/s)

it said transfer complete, but the file was transfer to where???

and then, how can i continue to get the info of that file.

Thanks

Read only

0 Likes
2,477

I guess that i solved it,

I used the FM FTP_SERVER_TO_R3.



CALL FUNCTION 'FTP_SERVER_TO_R3'
  EXPORTING
    handle               = mi_handle
    fname                = 'MM_OC_PEND_20110219.txt'
   CHARACTER_MODE       = '#'
* IMPORTING
*   BLOB_LENGTH          =
 TABLES
   BLOB                 = it_blob[]
   TEXT                 = it_text[]

the table it_text contains the data of the file 'MM_OC_PEND_20110219.txt'.

Thanks a lot to all of u guys.

Regards

Read only

0 Likes
2,477

Hello Miguel,

I have the same requirement.

I am doing following steps:

1) FTP Connect

2) FTP_SERVER_TO_R3

but I always have the error 'Command error'.

Can you help me if I am missing some thing please ?

Best Regards,
harish

Read only

0 Likes
2,477

Hello,

I got the issue I was not changing the DIR.

Thank you for this post !