- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
211 /*---- Start of User Written Code ----*/
212
213 data &_OUTPUT1.;
214 length
215 fref dref $8
216 fname $200
217 ;
218 rc = filename(dref,'/sas/config/compute/Lev1/SASLogs');
219 did = dopen(dref);
220 if did
7 The SAS System 08:53 Friday, July 30, 2021
221 then do;
222 do i = 1 to dnum(did);
223 fname = dread(did,i);
224 rc = filename(fref,’/sas/config/compute/Lev1/SASLogs/' !! fname);
225 fid = fopen(fref);
226 if fid
227 then do;
228 size = finfo(fid,foptname(fid,6));
229 modified = finfo(fid,foptname(fid,5));
230 output;
231 rc = fclose(fid);
232 end;
233 rc = filename(fref);
234 end;
235 rc = dclose(did);
236 end;
237 rc = filename(dref);
238 keep fname size modified;
239 run;
INFO: Character variables have defaulted to a length of 200 at the places given by: (Line):(Column). Truncation can result.
228:7 size
229:7 modified
ERROR: Resource is write-locked by another user. File
/sas/config/compute/Lev1/SASLogs/XXXX_2021.07.30_05.56.26.log. System Error Code = 11.
ERROR: Resource is write-locked by another user. File
=/sas/config/compute/Lev1/SASLogs/XXXX_2021.07.30_05.56.30.log. System Error Code = 11.
ERROR: Resource is write-locked by another user. File
=/sas/config/compute/Lev1/SASLogs/XXXX _2021.07.30_06.16.
25.log. System Error Code = 11.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)