Production Ready Macros for SAS Application Developers
mp_dirlist.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns all files and subdirectories within a specified parent
4  @details Not OS specific (uses dopen / dread). It does not appear to be
5  possible to reliably identify unix directories, and so a recursive
6  option is not available.
7  usage:
8 
9  %mp_dirlist(path=/some/location,outds=myTable);
10 
11  @param path= for which to return contents
12  @param outds= the output dataset to create
13 
14  @returns outds contains the following variables:
15  - file_or_folder (file / folder)
16  - filepath (path/to/file.name)
17  - filename (just the file name)
18  - ext (.extension)
19  - msg (system message if any issues)
20 
21  @version 9.2
22  @author Allan Bowe
23 **/
24 
25 %macro mp_dirlist(path=%sysfunc(pathname(work))
26  , outds=work.mp_dirlist
27 )/*/STORE SOURCE*/;
28 
29 data &outds (compress=no keep=file_or_folder filepath filename ext msg);
30  length filepath $500 fref $8 file_or_folder $6 filename $80 ext $20 msg $200;
31  rc = filename(fref, "&path");
32  if rc = 0 then do;
33  did = dopen(fref);
34  if did=0 then do;
35  putlog "NOTE: This directory is empty - &path";
36  msg=sysmsg();
37  put _all_;
38  stop;
39  end;
40  rc = filename(fref);
41  end;
42  else do;
43  msg=sysmsg();
44  put _all_;
45  stop;
46  end;
47  dnum = dnum(did);
48  do i = 1 to dnum;
49  filename = dread(did, i);
50  fid = mopen(did, filename);
51  if fid > 0 then do;
52  file_or_folder='file ';
53  ext = prxchange('s/.*\.{1,1}(.*)/$1/', 1, filename);
54  if filename = ext then ext = ' ';
55  end;
56  else do;
57  ext='';
58  file_or_folder='folder';
59  end;
60  filepath="&path/"!!filename;
61  output;
62  end;
63  rc = dclose(did);
64  stop;
65 run;
66 
67 %mend;