76%macro mp_dirlist(path=%sysfunc(pathname(work))
78 , outds=work.mp_dirlist
85%let getattrs=%upcase(&getattrs)XX;
86%let showparent=%upcase(&showparent)XX;
90%
if "%upcase(&sysscp)"=
"WIN" or
"%upcase(&sysscp)"=
"DNTHOST" %then %let mpsep=\;
93%local lvl nfront total room done mp_roots mpsep schema rc;
97%
if &fref ne 0 %then %let path=%sysfunc(pathname(&fref));
100%
if %length(&maxdepth)=0 or %sysfunc(verify(&maxdepth,0123456789))>0 %then %
do;
101 %
if "%upcase(&maxdepth)"=
"MAX" %then %let maxdepth=2147483647;
102 %
else %let maxdepth=0;
104%
if %length(&maxobs)=0 or %sysfunc(verify(&maxobs,0123456789))>0 %then %let maxobs=0;
107%mp_dropmembers(%scan(&outds,-1,.), libref=WORK)
110%let schema=work.%mf_getuniquename(prefix=mp_dirlist);
111%mddl_dirlist(&schema)
113 if 0 then
set &schema;
114 call missing(of _all_);
119%let fref1=%mf_getuniquefileref();
120%let fref2=%mf_getuniquefileref();
123%
if %length(&path)=0 %then %let mp_roots=1;
125%
if &mp_roots=1 %then %
do;
130 data work.__mp_dirlist_level;
131 if 0 then
set &schema;
132 length drive $3 letters $26;
133 call missing(of _all_);
134 %
if "%upcase(&sysscp)"=
"WIN" or
"%upcase(&sysscp)"=
"DNTHOST" %then %
do;
135 letters=
'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
137 drive=cats(substr(letters,i,1),
':');
138 rc=filename(
"&fref1",cats(drive,
'\'));
143 filepath=cats(drive,
'\');
145 file_or_folder=
'folder';
149 rc=filename(
"&fref1");
156 file_or_folder=
'folder';
160 keep directory filepath file_or_folder filename ext msg level;
163 proc append base=&outds data=work.__mp_dirlist_level nowarn force;
166 data work.__mp_dirlist_frontier;
167 set work.__mp_dirlist_level(where=(file_or_folder=
'folder'));
176 data work.__mp_dirlist_frontier;
177 length filepath $2000 level 8;
178 filepath=symget(
'path');
184%let nfront=%sysfunc(coalesce(%mf_nobs(work.__mp_dirlist_frontier),0));
187%
do %until(&nfront=0 or &lvl>&maxdepth or &done=1);
189 data work.__mp_dirlist_level;
190 if 0 then
set &schema;
191 set work.__mp_dirlist_frontier;
192 length sep $1 prefix $2000 fname $255 dmsg $200;
193 drop sep prefix fname dmsg rc did mid i;
199 rc=filename(
"&fref1", directory);
209 do while(length(directory)>1);
210 if substr(directory,length(directory),1) not in (
'/',
'\') then leave;
211 if length(directory)=3 and substr(directory,2,1)=
':' then leave;
212 directory=substr(directory,1,length(directory)-1);
215 if directory=
'/' or (length(directory)=3 and substr(directory,2,1)=
':')
216 then prefix=directory;
217 else prefix=cats(directory,sep);
221 filepath=cats(prefix,fname);
227 rc=filename(
"&fref2",filepath);
230 file_or_folder='folder';
235 if index(dmsg,'is not a directory')>0 then do;
236 file_or_folder='file';
237 ext=prxchange('s/.*\.{1,1}(.*)/$1/
',1,filename);
238 if filename=ext then ext=' ';
240 else if index(dmsg,'uthorization
')>0 then do;
241 /* present but not readable. It stays a file, as it was before the
242 entry test was fixed - the copy, hash and zip tools skip anything
243 that is not a file, and a backup that silently omits an unreadable
244 file is worse than one that fails on it */
245 file_or_folder='file
';
246 ext=prxchange('s/.*\.{1,1}(.*)/$1/
',1,filename);
247 if filename=ext then ext=' ';
250 else if fexist("&fref2") then do;
251 /* dopen refused it and the platform said nothing about why -
252 Viya returns an empty sysmsg() for a regular file, where other
253 platforms say it is not a directory. Anything that exists and
254 cannot be opened as a folder is a file: reporting it as locked
255 both hides it from callers that keep folders only and shows the
256 user a row with no message */
257 file_or_folder='file
';
258 ext=prxchange('s/.*\.{1,1}(.*)/$1/
',1,filename);
259 if filename=ext then ext=' ';
262 /* listed but not openable at all - a broken link, for instance */
263 file_or_folder='locked
';
267 rc=filename("&fref2");
272 rc=filename("&fref1");
275 /* trim the level to what is left of the maxobs budget before appending */
276 %let total=%sysfunc(coalesce(%mf_nobs(&outds),0));
277 %let room=2147483647;
278 %if &maxobs>0 %then %let room=%eval(&maxobs-&total);
279 %if &room>0 %then %do;
280 data work.__mp_dirlist_level;
281 set work.__mp_dirlist_level(obs=&room);
283 proc append base=&outds data=work.__mp_dirlist_level force nowarn;
287 %let total=%sysfunc(coalesce(%mf_nobs(&outds),0));
288 %if &maxobs>0 and &total>=&maxobs %then %let done=1;
290 /* the folders of this level become the parents of the next one */
292 %if &done=0 and &lvl<&maxdepth %then %do;
293 data work.__mp_dirlist_frontier;
294 set work.__mp_dirlist_level(where=(file_or_folder='folder
'));
298 %let nfront=%sysfunc(coalesce(%mf_nobs(work.__mp_dirlist_frontier),0));
301 %let lvl=%eval(&lvl+1);
305/* include the starting folder itself, when asked */
306%if %substr(&showparent,1,1)=Y %then %do;
307 data work.__mp_dirlist_level;
308 if 0 then set &schema;
309 /* the parent is only listed when it can be opened, so a path that does
310 not exist stays an empty result even with showparent=YES */
311 filepath=symget('path
');
312 rc=filename("&fref1",filepath);
314 if did>0 then rc=dclose(did);
315 rc=filename("&fref1");
318 do while(length(directory)>1);
319 if substr(directory,length(directory),1) not in ('/
','\
') then leave;
320 if length(directory)=3 and substr(directory,2,1)=':
' then leave;
321 directory=substr(directory,1,length(directory)-1);
324 file_or_folder='folder
';
326 filename=scan(directory,-1,'/\
');
332 proc append base=&outds data=work.__mp_dirlist_level force nowarn;
336%if %substr(&getattrs,1,1)=Y %then %do;
339 length infoname infoval $60 fref $8;
340 if _n_=1 then call missing(fref);
341 rc=filename(fref,filepath);
342 drop rc infoname fid i close fref;
343 if file_or_folder='file
' then do;
347 putlog "Could not open file:" filepath fid= ;
348 sasname='_MCNOTVALID_
';
351 else do i=1 to foptnum(fid);
352 infoname=foptname(fid,i);
353 infoval=finfo(fid,infoname);
354 sasname=compress(infoname, '_
', 'adik
');
355 if anydigit(sasname)=1 then sasname=substr(sasname,anyalpha(sasname));
356 if upcase(sasname) ne 'FILENAME
' then output;
364 putlog "Could not open folder:" filepath fid= ;
365 sasname='_MCNOTVALID_
';
368 else do i=1 to doptnum(fid);
369 infoname=doptname(fid,i);
370 infoval=dinfo(fid,infoname);
371 sasname=compress(infoname, '_
', 'adik
');
372 if anydigit(sasname)=1 then sasname=substr(sasname,anyalpha(sasname));
373 if upcase(sasname) ne 'FILENAME
' then output;
380 proc transpose data=&outds out=&outds(drop=_:);
383 by filepath file_or_folder filename ext ;
388 set &outds(where=(filepath ne ''));
391/* tidy up reserved filerefs, including those not used for an empty listing */
392%let rc=%sysfunc(filename(fref1));
393%let rc=%sysfunc(filename(fref2));
395drop table work.__mp_dirlist_frontier, work.__mp_dirlist_level, &schema;