DATA Step, Macro, Functions and more

Reading filenames, date and time from a directory

Reply
New Contributor
Posts: 2

Reading filenames, date and time from a directory

In Unix  I want to list all log files names in a folder along with last modified date and time separately.

 

(date and time can be in any format)

 

Expected output should be

 

file_name    date     time

 

file1           08-04     13:16

file2           08-04       13:17 

 

I tried below code, but not able to get date and time.

%let subdir = /abc/jht/temp/ ;
filename dir pipe "ls -1 &subdir | grep *.log ";
data Pfile;

length filename $ 300 size $ 20 time $ 20;
infile dir truncover;
input filename size time$;
run;
Super User
Posts: 10,610

Re: Reading filenames, date and time from a directory

Not sure if it could work .

 

infile dir truncover  expandtabs ;

 

Trusted Advisor
Posts: 1,822

Re: Reading filenames, date and time from a directory

It is not clear - is date and time part of the filename or do you want to extract them from the unix file metadata?

 

Look at the documentation of next functions: 

     DOPEN - open a directory

     DREAD - read a filename in the directory

     FOPEN - open a file

     FATTR  - get file attributes

     FCLOSE - close the file

     DCLOSE - clse the directory

 

Next code demonstartes usage of those functions:

%let path = /folders/myshortcuts/My_Folders/flat/;
filename mydir "&path";
data _NULL_;
  did = dopen('mydir');
  dnum = dnum(did);
  put did= dnum=;
  do i=1 to 4; /*dnum; */
     fname = dread(did,i);
     fnx = "&path"||fname;
     rc=filename('tmp',fname,,,'mydir');
     fid = fopen('tmp');
     put i= rc= fid= fnx=;
     if fid then do;
        fattr = finfo(fid,'Last Modified'); 
        lm = input(fattr,datetime18.);        
        put i= fattr=  lm= datetime18.;
      /*  
        infonum = foptnum(fid); put infonum=;
        do i=1 to infonum;
           infoname = foptname(fid,i);
           fattr = finfo(fid,foptname); put i= infoname = fattr=;
        end; 
      */
        fid=fclose(fid);
     end;
     rc=filename('tmp');
     rc=0; _syserr_ = 0;
  end;
run;

 

Ask a Question
Discussion stats
  • 2 replies
  • 99 views
  • 2 likes
  • 3 in conversation