Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_copyfolder.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief A macro to recursively copy a directory
4 @details Performs a recursive directory listing then works from top to bottom
5 copying files and creating subdirectories.
6
7 Usage:
8
9 %let rootdir=%sysfunc(pathname(work))/demo;
10 %let copydir=%sysfunc(pathname(work))/demo_copy;
11 %mf_mkdir(&rootdir)
12 %mf_mkdir(&rootdir/subdir)
13 %mf_mkdir(&rootdir/subdir/subsubdir)
14 data "&rootdir/subdir/example.sas7bdat";
15 run;
16
17 %mp_copyfolder(&rootdir,&copydir)
18
19 @param [in] source Unquoted path to the folder to copy from.
20 @param [out] target Unquoted path to the folder to copy to.
21 @param [in] copymax= (MAX) Set to a positive integer to indicate the level of
22 subdirectory copy recursion - eg 3, to go `./3/levels/deep`. For unlimited
23 recursion, set to MAX.
24
25 <h4> SAS Macros </h4>
26 @li mf_getuniquename.sas
27 @li mf_isdir.sas
28 @li mf_mkdir.sas
29 @li mp_abort.sas
30 @li mp_dirlist.sas
31
32 <h4> Related Macros </h4>
33 @li mp_copyfolder.test.sas
34
35**/
36
37%macro mp_copyfolder(source,target,copymax=MAX);
38
39 %mp_abort(iftrue=(%mf_isdir(&source)=0)
40 ,mac=&sysmacroname
41 ,msg=%str(Source dir does not exist (&source))
42 )
43
44 %mf_mkdir(&target)
45
46 %mp_abort(iftrue=(%mf_isdir(&target)=0)
47 ,mac=&sysmacroname
48 ,msg=%str(Target dir could not be created (&target))
49 )
50
51 /* prep temp table */
52 %local tempds;
53 %let tempds=%mf_getuniquename();
54
55 /* recursive directory listing */
56 %mp_dirlist(path=&source,outds=work.&tempds,maxdepth=&copymax)
57
58 /* create folders and copy content */
59 data _null_;
60 length msg $200;
61 call missing(msg);
62 set work.&tempds;
63 if _n_ = 1 then dpos+sum(length(directory),2);
64 filepath2="&target/"!!substr(filepath,dpos);
65 if file_or_folder='folder' then call execute('%mf_mkdir('!!filepath2!!')');
66 else do;
67 length fref1 fref2 $8;
68 rc1=filename(fref1,filepath,'disk','recfm=n');
69 rc2=filename(fref2,filepath2,'disk','recfm=n');
70 if fcopy(fref1,fref2) ne 0 then do;
71 msg=sysmsg();
72 putlog 'ERR' +(-1) "OR: Unable to copy " filepath " to " filepath2;
73 putlog msg=;
74 end;
75 end;
76 rc=filename(fref1);
77 rc=filename(fref2);
78 run;
79
80 /* tidy up */
81 proc sql;
82 drop table work.&tempds;
83
84%mend mp_copyfolder;