BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ANLYNG
Pyrite | Level 9

 

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

7 REPLIES 7
ballardw
Super User

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.

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lefunctionsref/p0cpuq4ew0dxipn1vtravlludjm7.h...

 

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)

Kurt_Bremser
Super User

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.

ANLYNG
Pyrite | Level 9

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?

SASKiwi
PROC Star

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.

ANLYNG
Pyrite | Level 9
here is some of the log - hope the errtos can be avoided. I get however data out when I run it now.

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.
AllanBowe
Barite | Level 11

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)
/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 3351 views
  • 1 like
  • 5 in conversation