Copy All Files From One Directory To Another
EDIT (2021): The mp_copyfolder macro in the SASjs Core library is now available, and is far preferable to the original example, for reasons such as:
- It will also include subdirectories (recursive function)
- There is no need for a datalines step (so will work inside a macro)
- It includes a test and documentation!
Source: https://github.com/sasjs/core/blob/main/base/mp_copyfolder.sas
Original Post (2013):
A platform agnostic copyfolder utility.. Just modify the in / out ROOT folders, and enter the corresponding copy FROM / copy TO directories in the cards file.
%let inroot=C:temp; /_ NO trailing slashes /
%let outroot=C:temp;
data null;
infile cards dsd;
input INDIR : $60. OUTDIR : $60. ;
call symput(cats(‘INDIR’,n),cats(”&inroot”,indir));
call symput(cats(‘OUTDIR’,n),cats(”&outroot”,outdir));
call symput(‘CNT’,put(n,8.));
cards4;/ list relative folders with NO leading or trailing slashes /
mxc,mxd
;;;;
run;
%macro copyFile(in,out);
/ these IN and OUT filerefs can point to any file /
filename in ”&in”;
filename out ”&out”;
/ copy the file byte-for-byte /
data _null;
length filein 8 fileid 8;
filein = fopen(‘in’,‘I’,1,‘B’);
fileid = fopen(‘out’,‘O’,1,‘B’);
rec = ‘20’x;
do while(fread(filein)=0);
rc = fget(filein,rec,1);
rc = fput(fileid, rec);
rc =fwrite(fileid);
end;
rc = fclose(filein);
rc = fclose(fileid);
run;
filename in clear;
filename out clear;
%mend CopyFile;
%macro getFilenames(location,dataset);
filename dir “%bquote(&location.)”;
data &dataset.(keep=memname);
handle=dopen( ’dir’ );
if handle > 0 then do;
count=dnum(handle);
do i=1 to count;
memname=dread(handle,i);
output;
end;
end;
rc=dclose(handle);
run;
filename dir clear;
%mend getFilenames;
%macro DoCopy();
%do x=1 %to &cnt;
%getFilenames(&&indir&x,ds&x);
data null;
set ds&x;
call execute(cats(‘%copyfile(&&indir&x’,memname,“,&&outdir&x”,memname,’)’));
run;
proc sql; drop table ds&x; quit;
%end;
%mend DoCopy;
%docopy;