mirror of
https://github.com/sasjs/core.git
synced 2025-12-10 22:14:35 +00:00
170 lines
4.9 KiB
SAS
170 lines
4.9 KiB
SAS
/**
|
|
@file
|
|
@brief Returns all files and subdirectories within a specified parent
|
|
@details When used with getattrs=NO, is not OS specific (uses dopen / dread).
|
|
|
|
If getattrs=YES then the doptname / foptname functions are used to scan all
|
|
properties - any characters that are not valid in a SAS name (v7) are simply
|
|
stripped, and the table is transposed so theat each property is a column
|
|
and there is one file per row. An attempt is made to get all properties
|
|
whether a file or folder, but some files/folders cannot be accessed, and so
|
|
not all properties can / will be populated.
|
|
|
|
Credit for the rename approach:
|
|
https://communities.sas.com/t5/SAS-Programming/SAS-Function-to-convert-string-to-Legal-SAS-Name/m-p/27375/highlight/true#M5003
|
|
|
|
|
|
usage:
|
|
|
|
%mp_dirlist(path=/some/location,outds=myTable)
|
|
|
|
%mp_dirlist(outds=cwdfileprops, getattrs=YES)
|
|
|
|
%mp_dirlist(fref=MYFREF)
|
|
|
|
@warning In a Unix environment, the existence of a named pipe will cause this
|
|
macro to hang. Therefore this tool should be used with caution in a SAS 9 web
|
|
application, as it can use up all available multibridge sessions if requests
|
|
are resubmitted.
|
|
If anyone finds a way to positively identify a named pipe using SAS (without
|
|
X CMD) do please raise an issue!
|
|
|
|
|
|
@param path= for which to return contents
|
|
@param fref= Provide a DISK engine fileref as an alternative to PATH
|
|
@param outds= the output dataset to create
|
|
@param getattrs= YES/NO (default=NO). Uses doptname and foptname to return
|
|
all attributes for each file / folder.
|
|
|
|
|
|
@returns outds contains the following variables:
|
|
- directory (containing folder)
|
|
- file_or_folder (file / folder)
|
|
- filepath (path/to/file.name)
|
|
- filename (just the file name)
|
|
- ext (.extension)
|
|
- msg (system message if any issues)
|
|
- OS SPECIFIC variables, if <code>getattrs=</code> is used.
|
|
|
|
@version 9.2
|
|
@author Allan Bowe
|
|
**/
|
|
|
|
%macro mp_dirlist(path=%sysfunc(pathname(work))
|
|
, fref=0
|
|
, outds=work.mp_dirlist
|
|
, getattrs=NO
|
|
)/*/STORE SOURCE*/;
|
|
%let getattrs=%upcase(&getattrs)XX;
|
|
|
|
data &outds(compress=no
|
|
keep=file_or_folder filepath filename ext msg directory
|
|
);
|
|
length directory filepath $500 fref fref2 $8 file_or_folder $6 filename $80
|
|
ext $20 msg $200;
|
|
%if &fref=0 %then %do;
|
|
rc = filename(fref, "&path");
|
|
%end;
|
|
%else %do;
|
|
fref="&fref";
|
|
rc=0;
|
|
%end;
|
|
if rc = 0 then do;
|
|
did = dopen(fref);
|
|
directory=dinfo(did,'Directory');
|
|
if did=0 then do;
|
|
putlog "NOTE: This directory is empty - " directory;
|
|
msg=sysmsg();
|
|
put _all_;
|
|
stop;
|
|
end;
|
|
rc = filename(fref);
|
|
end;
|
|
else do;
|
|
msg=sysmsg();
|
|
put _all_;
|
|
stop;
|
|
end;
|
|
dnum = dnum(did);
|
|
do i = 1 to dnum;
|
|
filename = dread(did, i);
|
|
filepath=cats(directory,'/',filename);
|
|
rc = filename(fref2,filepath);
|
|
midd=dopen(fref2);
|
|
dmsg=sysmsg();
|
|
if did > 0 then file_or_folder='folder';
|
|
rc=dclose(midd);
|
|
midf=fopen(fref2);
|
|
fmsg=sysmsg();
|
|
if midf > 0 then file_or_folder='file';
|
|
rc=fclose(midf);
|
|
|
|
if index(fmsg,'File is in use') or index(dmsg,'is not a directory')
|
|
then file_or_folder='file';
|
|
else if index(fmsg,'Insufficient authorization') then file_or_folder='file';
|
|
else if file_or_folder='' then file_or_folder='locked';
|
|
|
|
if file_or_folder='file' then do;
|
|
ext = prxchange('s/.*\.{1,1}(.*)/$1/', 1, filename);
|
|
if filename = ext then ext = ' ';
|
|
end;
|
|
else do;
|
|
ext='';
|
|
file_or_folder='folder';
|
|
end;
|
|
output;
|
|
end;
|
|
rc = dclose(did);
|
|
stop;
|
|
run;
|
|
|
|
%if %substr(&getattrs,1,1)=Y %then %do;
|
|
data &outds;
|
|
set &outds;
|
|
length infoname infoval $60 fref $8;
|
|
rc=filename(fref,filepath);
|
|
drop rc infoname fid i close fref;
|
|
if file_or_folder='file' then do;
|
|
fid=fopen(fref);
|
|
if fid le 0 then do;
|
|
msg=sysmsg();
|
|
putlog "Could not open file:" filepath fid= ;
|
|
sasname='_MCNOTVALID_';
|
|
output;
|
|
end;
|
|
else do i=1 to foptnum(fid);
|
|
infoname=foptname(fid,i);
|
|
infoval=finfo(fid,infoname);
|
|
sasname=compress(infoname, '_', 'adik');
|
|
if anydigit(sasname)=1 then sasname=substr(sasname,anyalpha(sasname));
|
|
if upcase(sasname) ne 'FILENAME' then output;
|
|
end;
|
|
close=fclose(fid);
|
|
end;
|
|
else do;
|
|
fid=dopen(fref);
|
|
if fid le 0 then do;
|
|
msg=sysmsg();
|
|
putlog "Could not open folder:" filepath fid= ;
|
|
sasname='_MCNOTVALID_';
|
|
output;
|
|
end;
|
|
else do i=1 to doptnum(fid);
|
|
infoname=doptname(fid,i);
|
|
infoval=dinfo(fid,infoname);
|
|
sasname=compress(infoname, '_', 'adik');
|
|
if anydigit(sasname)=1 then sasname=substr(sasname,anyalpha(sasname));
|
|
if upcase(sasname) ne 'FILENAME' then output;
|
|
end;
|
|
close=dclose(fid);
|
|
end;
|
|
run;
|
|
proc sort;
|
|
by filepath sasname;
|
|
proc transpose data=&outds out=&outds(drop=_:);
|
|
id sasname;
|
|
var infoval;
|
|
by filepath file_or_folder filename ext ;
|
|
run;
|
|
%end;
|
|
%mend; |