Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_deletefolder.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief A macro to delete a directory
4 @details Will delete all folder content (including subfolder content) and
5 finally, the folder itself.
6
7 Usage:
8
9 %let rootdir=%sysfunc(pathname(work))/demo;
10 %mf_mkdir(&rootdir)
11 %mf_mkdir(&rootdir/subdir)
12 %mf_mkdir(&rootdir/subdir/subsubdir)
13 data "&rootdir/subdir/example.sas7bdat";
14 run;
15
16 %mp_deletefolder(&rootdir)
17
18 @param [in] folder Unquoted path to the folder to delete.
19
20 <h4> SAS Macros </h4>
21 @li mf_getuniquename.sas
22 @li mf_isdir.sas
23 @li mp_dirlist.sas
24
25 <h4> Related Macros </h4>
26 @li mp_deletefolder.test.sas
27
28**/
29
30%macro mp_deletefolder(folder);
31 /* proceed if valid directory */
32 %if %mf_isdir(&folder)=1 %then %do;
33
34 /* prep temp table */
35 %local tempds;
36 %let tempds=%mf_getuniquename();
37
38 /* recursive directory listing */
39 %mp_dirlist(path=&folder,outds=work.&tempds, maxdepth=MAX)
40
41 /* sort descending level so can delete folder contents before folders */
42 proc sort data=work.&tempds;
43 by descending level;
44 run;
45
46 /* ensure top level folder is removed at the end */
47 proc sql;
48 insert into work.&tempds set filepath="&folder";
49
50 /* delete everything */
51 data _null_;
52 set work.&tempds end=last;
53 length fref $8;
54 fref='';
55 rc=filename(fref,filepath);
56 rc=fdelete(fref);
57 if rc then do;
58 msg=sysmsg();
59 put "&sysmacroname:" / rc= / msg= / filepath=;
60 end;
61 rc=filename(fref);
62 run;
63
64 /* tidy up */
65 proc sql;
66 drop table work.&tempds;
67
68 %end;
69 %else %put &sysmacroname: &folder: is not a valid / accessible folder. ;
70%mend mp_deletefolder;