I need to a list of files in a specific directory with names and timestamps.
I have the below code. But it only outputs filenames and not other wanted information eg. created or changed timestamp. Nice to have as well size of the files.
data &_OUTPUT1.;
length fref $8 fname $200;
did = filename(fref,'/sas/data/Logs');
did = dopen(fref);
do i = 1 to dnum(did);
fname = dread(did,i);
output;
end;
did = dclose(did);
did = filename(fref);
keep fname;
run;
Thanks in advance.
This is an example that works on UNIX (as University Edition runs on a Linux instance):
data fdata;
length
fref dref $8
fname $200
;
rc = filename(dref,'/folders/myfolders');
did = dopen(dref);
if did
then do;
do i = 1 to dnum(did);
fname = dread(did,i);
rc = filename(fref,'/folders/myfolders/' !! fname);
fid = fopen(fref);
if fid
then do;
size = finfo(fid,foptname(fid,6));
modified = finfo(fid,foptname(fid,5));
output;
rc = fclose(fid);
end;
rc = filename(fref);
end;
rc = dclose(did);
end;
rc = filename(dref);
keep fname size modified;
run;
You will have to convert the resulting character values to numbers.
Look at the documentation for the FINFO function. Once you have an individual file identifier you use that function to get the bits of information that are available.
The operating system determines which items are available and in what order.
Or parse output from an OS command like DIR (windows) or LS (unix/linux)
This is an example that works on UNIX (as University Edition runs on a Linux instance):
data fdata;
length
fref dref $8
fname $200
;
rc = filename(dref,'/folders/myfolders');
did = dopen(dref);
if did
then do;
do i = 1 to dnum(did);
fname = dread(did,i);
rc = filename(fref,'/folders/myfolders/' !! fname);
fid = fopen(fref);
if fid
then do;
size = finfo(fid,foptname(fid,6));
modified = finfo(fid,foptname(fid,5));
output;
rc = fclose(fid);
end;
rc = filename(fref);
end;
rc = dclose(did);
end;
rc = filename(dref);
keep fname size modified;
run;
You will have to convert the resulting character values to numbers.
It is a nice solution. however it display an error with this message Code = 11. - ERROR: Resource is write-locked by another user. File ****System Error Code = 11. I thinks it has problems reading information for files which is in use. it is log files on a running system. is there an easy way to exclude the running files and display the rest?
Please post your SAS log - what statement causes that error?
It should be possible to test for that error to avoid the DATA step failing.
Here's a macro that will work on both UNIX and WINDOWS and does not require XCMD:
https://core.sasjs.io/mp__dirlist_8sas.html
To invoke:
%mp_dirlist(path=/your/dir, outds=work.ds, getattrs=YES)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.