Hello,
I have used part of this program to create a file to read a directory and list all the files and their paths. However, I would like to add a part which will add a column which tells me when the file was created and also when it was last modified.
Would someone be able to help me amend this please?
data dirs_found (compress=no);
length Root $120.;
root = "/my directory";
output;
run;
data
dirs_found /* Updated list of directories searched */
files_found (compress=no); /* Names of files found. */
keep Path FileName FileType modified;
length fref $8 Filename $120 FileType $16 ;
/* Read the name of a directory to search. */
modify dirs_found;
/* Make a copy of the name, because we might reset root. */
Path = root;
/* For the use and meaning of the FILENAME, DOPEN, DREAD, MOPEN, and */
/* DCLOSE functions, see the SAS OnlineDocs. */
rc = filename(fref, path);
if rc = 0 then
do;
did = dopen(fref);
rc = filename(fref);
end;
else
do;
length msg $200.;
msg = sysmsg();
putlog msg=;
did = .;
end;
if did <= 0
then
do;
putlog 'ERR' 'OR: Unable to open ' Path=;
return;
end;
dnum = dnum(did);
do i = 1 to dnum;
filename = dread(did, i);
fid = mopen(did, filename);
/* It's not explicitly documented, but the SAS online */
/* examples show that a return value of 0 from mopen */
/* means a directory name, and anything else means */
/* a file name. */
if fid > 0
then
do;
/* FileType is everything after the last dot. If */
/* no dot, then no extension. */
FileType = prxchange('s/.*\.{1,1}(.*)/$1/', 1, filename);
if filename = filetype then filetype = ' ';
output files_found;
end;
else
do;
/* A directory name was found; calculate the complete */
/* path, and add it to the dirs_found data set, */
/* where it will be read in the next iteration of this */
/* data step. */
root = catt(path, "\", filename);
output dirs_found;
end;
end;
rc = dclose(did);
run;
I suggest you to use code from @Kurt_Bremser's "Talking to Your Host" article from WUSS 2022 (https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/8...)
The code would be like this:
%let filesLocation=C:\SAS_WORK\;
/* get full list of files from &filesLocation. (including all subdirectories) */
data files_to_get; /* just to be sure the file "files" is "empty" */
run;
data files_to_get;
length root dname $ 2048 filename $ 256 dir level 8;
root = "&filesLocation.";
retain filename dname ' ' level 0 dir 1;
label
filename = "file"
dname = "folder"
;
run;
data files_to_get;
modify files_to_get;
rc1=filename('tmp',catx('/',root,dname,filename));
rc2=dopen('tmp');
dir = 1 & rc2;
if dir then
do;
dname=catx('/',dname,filename);
filename=' ';
end;
replace;
if dir;
level=level+1;
do i=1 to dnum(rc2);
filename=dread(rc2,i);
output;
end;
rc3=dclose(rc2);
run;
/* get files info in "Long" format */
data files_to_get_dates;
set files_to_get(where=(dir=0));
fname="tempfile";
rc=filename(fname, catx('/',root,dname,filename));
if rc = 0 and fexist(fname) then
do;
fid=fopen(fname);
infonum=foptnum(fid);
do i=1 to infonum;
infoname=foptname(fid, i);
infoval=finfo(fid, infoname);
output;
end;
fid=fclose(fid);
end;
rc=filename(fname);
drop dir--i;
run;
/* transforms files info into "Wide" format */
proc transpose
data = files_to_get_dates(rename=(filename=fn))
out=files_to_get_dates_T(drop =_name_);
by root dname fn notsorted;
id infoname;
var infoval;
run;
Bart
I suggest you to use code from @Kurt_Bremser's "Talking to Your Host" article from WUSS 2022 (https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/8...)
The code would be like this:
%let filesLocation=C:\SAS_WORK\;
/* get full list of files from &filesLocation. (including all subdirectories) */
data files_to_get; /* just to be sure the file "files" is "empty" */
run;
data files_to_get;
length root dname $ 2048 filename $ 256 dir level 8;
root = "&filesLocation.";
retain filename dname ' ' level 0 dir 1;
label
filename = "file"
dname = "folder"
;
run;
data files_to_get;
modify files_to_get;
rc1=filename('tmp',catx('/',root,dname,filename));
rc2=dopen('tmp');
dir = 1 & rc2;
if dir then
do;
dname=catx('/',dname,filename);
filename=' ';
end;
replace;
if dir;
level=level+1;
do i=1 to dnum(rc2);
filename=dread(rc2,i);
output;
end;
rc3=dclose(rc2);
run;
/* get files info in "Long" format */
data files_to_get_dates;
set files_to_get(where=(dir=0));
fname="tempfile";
rc=filename(fname, catx('/',root,dname,filename));
if rc = 0 and fexist(fname) then
do;
fid=fopen(fname);
infonum=foptnum(fid);
do i=1 to infonum;
infoname=foptname(fid, i);
infoval=finfo(fid, infoname);
output;
end;
fid=fclose(fid);
end;
rc=filename(fname);
drop dir--i;
run;
/* transforms files info into "Wide" format */
proc transpose
data = files_to_get_dates(rename=(filename=fn))
out=files_to_get_dates_T(drop =_name_);
by root dname fn notsorted;
id infoname;
var infoval;
run;
Bart
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.