-1
DEFINE VARIABLE cDir  AS CHARACTER NO-UNDO initial '/home/raj/'.

define temp-table tt-file
       field tfile as char format "x(22)".
define variable vfile as char format "x(22)" no-undo.

INPUT FROM OS-DIR (cDir) ECHO.

REPEAT:

    create tt-file.
    IMPORT tt-file.tfile.
        FILE-INFO:FILE-NAME = cDir + tt-file.tfile.
      if file-info:file-name begins cdir + "RATES"
         and string(month(file-info:file-mod-date)) = string(month(today)) THEN    
     DISPLAY tt-file.tfile FORMAT "X(22)" LABEL 'name of the file'
             FILE-INFO:FULL-PATHNAME FORMAT "X(21)" LABEL 'FULL-PATHNAME'
             FILE-INFO:PATHNAME FORMAT "X(30)" LABEL 'PATHNAME'
             FILE-INFO:FILE-TYPE FORMAT "X(5)" LABEL 'FILE-TYPE'
             file-info:file-mod-date.
end.

The problem I face is I have multiple files for the current month but I need to get latest file for the month.

Jensd
  • 7,886
  • 2
  • 28
  • 37
user3715001
  • 131
  • 3
  • 18

2 Answers2

3

Why not add two fields for file creation: date & time? Then you can easily write a simple WHERE-clause that get's you the last file between two dates.

DEFINE VARIABLE cDir  AS CHARACTER NO-UNDO INITIAL '/home/raj/'.

DEFINE TEMP-TABLE tt-file NO-UNDO
       FIELD tfile      AS CHARACTER FORMAT "x(22)"
       FIELD createDate AS DATE
       FIELD createTime AS INTEGER.

INPUT FROM OS-DIR (cDir).
REPEAT:
    CREATE tt-file.
    IMPORT tt-file.tfile.

    FILE-INFO:FILE-NAME = cDir + tt-file.tfile.

    ASSIGN 
        tt-file.createDate = FILE-INFO:FILE-CREATE-DATE
        tt-file.createtime = FILE-INFO:FILE-CREATE-TIME.
END.

/* Here you could add any logic to get your file */
FOR EACH tt-file NO-LOCK BY tt-file.createDate BY tt-file.createtime:
    DISP tt-file.
END.

/* Updated example for doing things only with .csv files */
FOR EACH tt-file NO-LOCK WHERE tt-file.tfile MATCHES "*csv" 
                            BY tt-file.createDate 
                            BY tt-file.createtime:
    DISP tt-file.
END.

Depending on your application you might want to use FILE-MOD-DATE & FILE-MOD-TIME instead.

Jensd
  • 7,886
  • 2
  • 28
  • 37
  • 1
    Thanks jenscd.i got the latest file using break by. For each tt-file where tfile begins "RATES" and tt-file.tfiletype = "FRW" NO-LOCK break BY tt-file.createDate BY tt-file.createtime: if last(createdate) then DISP tt-file with 2 col. END – user3715001 Jul 07 '14 at 15:11
  • I am able to get the latest file wHich starts with "TAXRATES" BUT I ALSO WANT TO HAVE ONLY ".CSV" FILES.hOW DO I DO THAT? – user3715001 Jul 16 '14 at 11:26
  • 1
    Thanks jensd.I Used a word index in temp-table and used contains word to flter .csv files. – user3715001 Jul 16 '14 at 13:02
0

Your sample shows you are using unix/linux. You could use os-command

define variable cDir as character no-undo form "x(65)" initial '/home/raj/'.
define variable cFileName as character no-undo form "x(65)".
input through value(substitute("ls -1t &1", cDir)).

repeat:
    import unformatted cFileName.
    display cFileName.
    /* if .... then */ leave.
end.

Instead of ls you could also process the results of a find command. This will require a bit more code but is much more powerful.

carl verbiest
  • 1,113
  • 1
  • 15
  • 30