Application Development 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: 

Need to read received date of a file that is on FTP server

Former Member
0 Kudos

Hi,

Can anyone share some thoughts on how we can read a received date of a file that is on a FTP server.

Any one function module we have , Please let me know .

Regards,

Ry

1 ACCEPTED SOLUTION

Former Member
0 Kudos

I guess you can get by using following way :

data: begin of t_tabl occurs 0,

line(132),

end of t_tabl,

data: lc_command(100) type c.

  • Get all the file name falling under specified directory...

lc_command(3) = 'ls '.

lc_command+3(45) = p_dir. " Directory of file path

call 'SYSTEM' id 'COMMAND' field lc_command

id 'TAB' field t_tabl-sys.

  • Check any files exits in the directory.......................

if t_tabl[] is initial.

message e006 with 'No files exist in the specified directory ' p_dir.

endif.

Thanks

Seshu

13 REPLIES 13

Former Member
0 Kudos

Hi Ray,

To Read a file on FTP server:

1. Open the File.

OPEN DATASET <infile> FOR INPUT IN TEXT MODE

ENCODING DEFAULT.

IF sy-subrc <> 0. "Check the return code

MESSAGE "File Cannot be opened" WITH <infile>.

ENDIF.

2. Read the contents of the file.

read dataset <infile> into <wa>.

and do the required processing with the contents of the file.

3.Close the file after using it.

CLOSE DATASET <infile>.

IF sy-subrc <> 0. "Check the return code

MESSAGE "File cannot be closed", WITH <infile>.

ENDIF.

Regards,

Vinod.

0 Kudos

vinod , thanks for the info , but my question is not reading the file , my point how do we read the <b>date</b> of the received file on the FTP server.

example : xyz received on 12/31/2005.

i am trying to access the file on 01/01/2006 . i need to retreive the date 12/31/2005 .

let me know .

Regards,

Ry

Former Member
0 Kudos

I guess you can get by using following way :

data: begin of t_tabl occurs 0,

line(132),

end of t_tabl,

data: lc_command(100) type c.

  • Get all the file name falling under specified directory...

lc_command(3) = 'ls '.

lc_command+3(45) = p_dir. " Directory of file path

call 'SYSTEM' id 'COMMAND' field lc_command

id 'TAB' field t_tabl-sys.

  • Check any files exits in the directory.......................

if t_tabl[] is initial.

message e006 with 'No files exist in the specified directory ' p_dir.

endif.

Thanks

Seshu

0 Kudos

Seshu , thanks for the info, can give me an example with exact parameters , thanks .

Ry

0 Kudos

data: begin of t_tabl occurs 0,

line(132),

end of t_tabl.

data: lc_command(100) type c.

start-of-selection.

  • Get all the file name falling under specified directory...

lc_command(3) = 'ls -lrt '.

lc_command+3(45) = p_dir. " Directory of file path -> give ur file path

call 'SYSTEM' id 'COMMAND' field lc_command

id 'TAB' field t_tabl-sys.

  • Check any files exits in the directory.......................

if t_tabl[] is initial.

message e006 with 'No files exist in the specified directory ' p_dir.

endif.

see the t_tabl value in debugging

Thanks

Seshu

0 Kudos

Seshu, thanks , but one more question.

lc_command(3) = 'ls -lrt '. what is ls - lrt please let me know .

Regards,

Ry

0 Kudos

I tested the program and it works fine ..

data: begin of t_tabl occurs 0,

line(132),

end of t_tabl.

data: lc_command(100) type c.

start-of-selection.

  • Get all the file name falling under specified directory...

<b>lc_command(7)</b> = 'ls -lrt '. -> this will give list of files under directory with date

<b>lc_command+8(45)</b> = '/usr/sap/DEV/SYS/profile'." Directory of file path

call 'SYSTEM' id 'COMMAND' field lc_command

id 'TAB' field t_tabl-sys.

loop at t_tabl.

break sy-uname.

endloop.

within loop read the data with ur file ,then take the date value.

Thanks

Seshu

0 Kudos

seshu,

i have written as you said .

but in the table i can see only the file but no date in it . am i missing some thing.

here is the code i wrote :

data: begin of t_tabl occurs 0,

line(132),

end of t_tabl.

data: lc_command(100) type c.

  • Get all the file name falling under specified directory...

lc_command(3) = 'ls -lrt '.

lc_command+3(45) = '/export/XYZtf/test'. " Directory of file path

call 'SYSTEM' id 'COMMAND' field lc_command

id 'TAB' field t_tabl-sys.

  • Check any files exits in the directory.......................

if t_tabl[] is initial.

*message e006 with 'No files exist in the specified directory ' p_dir.

endif.

lc_command shows as ls /export/xyztf/test.

t_tabl - line gives the file name .

where do i check the date , plase let me know .

Regards,

Ry

0 Kudos

Hello Roby,

See the bold one :

data: begin of t_tabl occurs 0,

line(132),

end of t_tabl.

data: lc_command(100) type c.

  • Get all the file name falling under specified directory...

lc_command(<b>7</b>) = 'ls -lrt'.

lc_command+<b>8</b>(45) = '/export/XYZtf/test'. " Directory of file path

call 'SYSTEM' id 'COMMAND' field lc_command

id 'TAB' field t_tabl-sys.

loop at t_tabl.

-> keep break point and see t_tabl value

endloop.

0 Kudos

Seshu,

Thanks a lot for the help , i got the date now .

Thanks you all for the info .

Regards,

Ry

former_member194669
Active Contributor
0 Kudos

Hi,

May be you can try with fm

EPS_GET_FILE_ATTRIBUTES

Please remember put check in Upper/lower case while testing

(due to case sensitive)

former_member194669
Active Contributor
0 Kudos

Hi,

Lists the files and subdirectories in the present directory. The -lrt gives a long format in reverse time order.

aRs

Clemenss
Active Contributor
0 Kudos

Hi Roby,

enough rubbish.

I think you can read the ftp directory listing using ftp DIR or ls - l command. In the filellist returned, you can extract the file date and time (which is receive date and time).

Following is a program we used to transfer files from ftp to server and then download them. We checked the directory listing lines to get the files we need.

Check the form ftp_get_files: We loop at the gt_data table that contains the result of LIST (or ls) command. Instead you must use the 'dir' command which will return lines like


-rw-r--r--   1 clems    users        2917 Jun  9 22:00 _tn_P5290021.JPG

Check the offsets accordingly and reformat the time stamp to a useful format (If you need code, I could search for it).

Here the ftp stuff [as is]

*----------------------------------------------------------------------*
* Object name................:
* Transport request..........:
* Author.....................: Clemens Li
* Contact person.op. departm.:
* Creation date..............:
* Copied from................:
* Functional description.....:
*
*----------------------------------------------------------------------*
* Change history:
*----------------------------------------------------------------------*
* Date        Change reuest #  Autor                      ID
*             Description
*----------------------------------------------------------------------*
* DD.MM.YYYY  T00K......       Name                       NNYYYYMMDD
*             .....................
*----------------------------------------------------------------------*

REPORT  z_ftp_get  .                   .
DATA:
  gt_data   TYPE TABLE OF text1024,
  gv_folder TYPE string,
  gv_handl  TYPE i.
PARAMETERS:
  p_host     TYPE rfchost DEFAULT 'xxxxx' LOWER CASE OBLIGATORY,
  p_user     TYPE syuname DEFAULT 'xxxxx' LOWER CASE OBLIGATORY,
  p_pass     TYPE file    DEFAULT 'xxxxx' LOWER CASE OBLIGATORY
    ,
  p_rfcdes   TYPE rfcdest DEFAULT 'SAPFTPA' OBLIGATORY,
  p_rcd      TYPE file    DEFAULT 'xxxxx',
  p_tmpdr    TYPE fileintern
    DEFAULT 'xxxxx',
  p_lcd      TYPE file,
  p_prefix   TYPE file    DEFAULT 'xxxxx',
  p_list     TYPE flag RADIOBUTTON GROUP func,
  p_down     TYPE flag RADIOBUTTON GROUP func.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_lcd.
  PERFORM f4_pcdir CHANGING p_lcd.

INITIALIZATION.
  PERFORM initialization.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    CHECK screen-name = 'P_PASS'.
    screen-invisible = '1'.
    MODIFY SCREEN.
  ENDLOOP.

START-OF-SELECTION.
  PERFORM ftp_open CHANGING gv_handl.
  PERFORM ftp_command USING gv_handl:
    'cd' p_rcd CHANGING gt_data,
    'ls' ''    CHANGING gt_data.
  CASE 'X'.
    WHEN p_list.
      PERFORM ftp_list_files USING gt_data.
    WHEN p_down.
    PERFORM ftp_get_files USING gv_handl gt_data p_prefix p_tmpdr p_lcd.
  ENDCASE." 'X'.


*&---------------------------------------------------------------------*
*&      Form  ftp_open
*&---------------------------------------------------------------------*
FORM ftp_open CHANGING pv_handl.
  DATA:
    lv_pass    TYPE text40,
    lv_key     TYPE i VALUE 26101957,
    lv_dstlen  TYPE i,
    lv_subrc   TYPE sysubrc,
    lv_direc   TYPE file.

  CALL 'AB_RFC_X_SCRAMBLE_STRING'
        ID 'SOURCE'  FIELD p_pass      ID 'KEY'         FIELD lv_key
        ID 'SCR'     FIELD 'X'         ID 'DESTINATION' FIELD lv_pass
        ID 'DSTLEN'  FIELD lv_dstlen.


  CLEAR:
    pv_handl.
  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user                   = p_user
      password               = lv_pass
*   ACCOUNT                =
      host                   = p_host
      rfc_destination        = p_rfcdes
*   GATEWAY_USER           =
*   GATEWAY_PASSWORD       =
*   GATEWAY_HOST           =
  IMPORTING
    handle                 = pv_handl
  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.

ENDFORM.                    " ftp_open
*&---------------------------------------------------------------------*
*&      Form  ftp_command
*&---------------------------------------------------------------------*
FORM ftp_command
  USING   pv_handl TYPE i
          pv_comnd TYPE file
          pv_optns TYPE file
  CHANGING pt_data  TYPE table.     .
  DATA:
    lv_command TYPE file.
  FREE: pt_data.
  CONCATENATE pv_comnd  pv_optns INTO lv_command
    SEPARATED BY space.
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle              = pv_handl
      command             = lv_command
*   COMPRESS            =
*   VERIFY              =
* IMPORTING
*   FILESIZE            =
*   FILEDATE            =
*   FILETIME            =
    TABLES
      data                = pt_data
    EXCEPTIONS
      tcpip_error         = 1
      command_error       = 2
      data_error          = 3
      OTHERS              = 4
            .
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(HANDLE) TYPE  I
*"     VALUE(COMMAND) TYPE  C
*"     VALUE(COMPRESS) TYPE  C OPTIONAL
*"     VALUE(VERIFY) TYPE  C OPTIONAL
*"  EXPORTING
*"     VALUE(FILESIZE) TYPE  I
*"     VALUE(FILEDATE) LIKE  SY-DATUM
*"     VALUE(FILETIME) LIKE  SY-UZEIT
*"  TABLES
*"      DATA
*"  EXCEPTIONS
*"      TCPIP_ERROR
*"      COMMAND_ERROR
*"      DATA_ERROR

  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.                    " ftp_command
*&---------------------------------------------------------------------*
*&      Form  ftp_get_files
*&---------------------------------------------------------------------*
FORM ftp_get_files
  USING
    pv_handl   TYPE i
    pt_data    TYPE table
    p_prefix   TYPE file
    p_tmpdr    TYPE fileintern
    p_lcd      TYPE file.
  FIELD-SYMBOLS:
    <fs_any> TYPE ANY.
  DATA:
    lt_data   TYPE TABLE OF text1024,
    lv_tmpdr  TYPE file,
    lv_tmpfil TYPE sapb-sappfad,
    lv_file   TYPE file,
    lv_cmd    TYPE file,
    lv_lcfil  TYPE sapb-sappfad.
  CALL FUNCTION 'FILE_GET_NAME'
       EXPORTING
            logical_filename = p_tmpdr
       IMPORTING
            file_name        = lv_tmpdr
       EXCEPTIONS
            file_not_found   = 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.

  LOOP AT pt_data ASSIGNING <fs_any>.
    CHECK <fs_any> CS p_prefix.
    lv_file = <fs_any>+sy-fdpos.
    CHECK lv_file CA space.
    lv_file = lv_file(sy-fdpos).
    CONCATENATE lv_tmpdr lv_file INTO lv_tmpfil.
    CONCATENATE  lv_file lv_tmpfil INTO lv_cmd SEPARATED BY space.
    PERFORM ftp_command USING pv_handl 'get' lv_cmd
      CHANGING lt_data.
    CONCATENATE p_lcd '' lv_file  INTO lv_lcfil.
    CALL FUNCTION 'ARCHIVFILE_SERVER_TO_CLIENT'
         EXPORTING
              path       = lv_tmpfil
              targetpath = lv_lcfil
         EXCEPTIONS
              error_file = 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.
    ELSE.
      DELETE DATASET lv_tmpfil.
      CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
        EXPORTING
*         PERCENTAGE       = 0
          text             = lv_lcfil.

    ENDIF.

  ENDLOOP." at pt_data assigning <fs_any>.
ENDFORM.                    " ftp_get_files
*&---------------------------------------------------------------------*
*&      Form  initialization
*&---------------------------------------------------------------------*
FORM initialization.
  DATA:
    lv_dir TYPE string.
  CALL METHOD cl_gui_frontend_services=>directory_get_current
    CHANGING
      current_directory            = lv_dir.

  CALL METHOD cl_gui_cfw=>flush.
  p_lcd = lv_dir.
ENDFORM.                    " initialization
*&---------------------------------------------------------------------*
*&      Form  f4_pcdir
*&---------------------------------------------------------------------*
FORM f4_pcdir CHANGING pv_dir TYPE file.
  CLEAR gv_folder.
  CALL METHOD cl_gui_frontend_services=>directory_browse
     EXPORTING
       window_title    = 'Frontend Directory'
       initial_folder  = 'C:'
    CHANGING
      selected_folder = gv_folder.
  CALL METHOD cl_gui_cfw=>flush.
  CHECK NOT gv_folder IS INITIAL.
  pv_dir = gv_folder.
ENDFORM.                                                    " f4_pcdir
*&---------------------------------------------------------------------*
*&      Form  ftp_list_files
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_DATA  text
*----------------------------------------------------------------------*
FORM ftp_list_files USING    pt_data TYPE table.
  FIELD-SYMBOLS:
    <fs_any> TYPE ANY.
  LOOP AT pt_data ASSIGNING <fs_any>.
    CHECK <fs_any> CS p_prefix.
    WRITE: / <fs_any>.
  ENDLOOP." AT pt_data ASSIGNING <fs_any>.
ENDFORM.                    " ftp_list_files

Regards,

Clemens