-1

How to fetch user details for a .sas file or file properties for all files stored in a directory? I am trying to get all possible attributes like: modified date, modified by, created by, for a macro.

data dir_meta(drop=rc file_ref fid);

%let directory_ref = %sysfunc(filename(dirref,&dir));
%let dir_id=%sysfunc(dopen(&dirref));

if &dir_id eq 0 then do;
 put _error_=1;
 return;
end;

%let _count=%sysfunc(dnum(&dir_id);

do i=1 to &_count;
 %let dir_name = %sysfunc(dread(&dir_id,&i);
  if upcase(scan(&dir_name,-1,.)) = upcase(&extn) then do;
        put &dir\&dir_name;
        file_ref='temp';
        file_name=%sysfunc( filename(file_ref,"&dir\&&dir_name"));
        fid=%sysfunc(fopen(file_ref));
        create_date=%sysfunc(finfo(&fid,Create Time));
        Modified_date=%sysfunc(finfo(&fid,Last Modified));
        output;
        rc=fclose(fid);
    end;
end;
 %let rc_dir=%sysfunc(dclose(dir_id);
run;
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Please explain what issue are you experiencing with that code. It already looks like you are mixing data step logic with macro logic in a way that I would be surprised to hear that works. – user2877959 Sep 25 '17 at 10:01
  • Proc contents might help? http://documentation.sas.com/?docsetId=proc&docsetVersion=9.4&docsetTarget=p06aw5wr49vg04n1n0iqzalvqss0.htm&locale=en – pinegulf Sep 25 '17 at 10:05
  • That's nice. What's the question? If you want help you need to provide more details, but for starters, FINO is limited in the information it can provide so I don't think it will provide what you're looking for: http://support.sas.com/documentation/cdl/en/hostwin/69955/HTML/default/viewer.htm#n0vegoaf5t6s31n1wu1qok4swqye.htm – Reeza Sep 25 '17 at 15:43

1 Answers1

0

Sweta,

Presuming you are using SAS in a recent version of Windows and the session has X command allowed, then you can pipe the results of a powershell command to a data step to read in what ever information you want.

In powershell use this command to see the kinds of information about a file that can be selected

PS > DIR | GET-MEMBER

Once you decide on the members to select a data step can read the powershell output. For example:

filename fileinfo pipe 'powershell -command "dir | select Fullname, Length, @{E={$_.LastWriteTime.ToString(''yyyy-MM-ddTHH:mm:ss.ffffffzzz'')}} | convertTo-csv"';
* powershell datetime formatting tips: https://technet.microsoft.com/en-us/library/ee692801.aspx?f=255&MSPPError=-2147217396;

data mydata;
  infile fileinfo missover firstobs=4 dsd dlm=',';
  attrib 
    filename length=$250
    size length=8 format=comma12.
    lastwrite length=8 format=datetime20. informat=E8601DZ32.6
  ;
  input filename size lastwrite;
run;
Richard
  • 25,390
  • 3
  • 25
  • 38