Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_tree.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Recursively scans a directory tree to get all subfolders and content
4 @details
5 Usage:
6
7 %mp_tree(dir=/tmp, outds=work.tree)
8
9 Credits:
10
11 Roger Deangelis:
12https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332616/highlight/true#M74887
13
14 Tom:
15https://communities.sas.com/t5/SAS-Programming/listing-all-files-of-all-types-from-all-subdirectories/m-p/334113/highlight/true#M75419
16
17
18 @param [in] dir= (/tmp) Directory to be scanned
19 @param [out] outds= (work.mp_tree) Dataset to create
20
21 @returns outds contains the following variables:
22
23 - `dir`: a flag (1/0) to say whether it is a directory or not. This is not
24 reliable - folders that you do not have permission to open will be flagged
25 as directories.
26 - `ext`: file extension
27 - `filename`: file name
28 - `dirname`: directory name
29 - `fullpath`: directory + file name
30
31 @version 9.2
32 @sasjslint {"maxLineLength": 135}
33**/
34
35%macro mp_tree(dir=/tmp
36 ,outds=work.mp_tree
37)/*/STORE SOURCE*/;
38
39data &outds ;
40 length dir 8 ext filename dirname $256 fullpath $512 ;
41 call missing(of _all_);
42 fullpath = "&dir";
43run;
44
45%local sep;
46%if &sysscp=WIN or &SYSSCP eq DNTHOST %then %let sep=\;
47%else %let sep=/;
48
49data &outds ;
50 modify &outds ;
51 retain sep "&sep";
52 rc=filename('tmp',fullpath);
53 dir_id=dopen('tmp');
54 dir = (dir_id ne 0) ;
55 if dir then dirname=fullpath;
56 else do;
57 filename=scan(fullpath,-1,sep) ;
58 dirname =substrn(fullpath,1,length(fullpath)-length(filename));
59 if index(filename,'.')>1 then ext=scan(filename,-1,'.');
60 end;
61 replace;
62 if dir then do;
63 do i=1 to dnum(dir_id);
64 fullpath=cats(dirname,sep,dread(dir_id,i));
65 output;
66 end;
67 rc=dclose(dir_id);
68 end;
69 rc=filename('tmp');
70run;
71
72%mend mp_tree;