Hi,
I came across this great macro on the internet to zip all files within a specific folder. It's great, but I would like to add a parameter to this macro which enables me to enter a particular extension, let's say XLSX. How should I change the macro below ? I've been fiddling with it, but cannot get it right. Help would be greatly appreciated
%let n=0;
%macro readCatalog(path, localpath);
%local rc _path filrf did noe filename fid i;
%if &localpath = %then
%let _path=&path;
%else
%let _path=&path\&localpath;
%let n=%eval(&n+1);
%let filrf=DIR&n;
%let rc = %sysfunc(filename(filrf, &_path));
%let did = %sysfunc(dopen(&filrf));
%let noe = %sysfunc(dnum(&did));
%do i = 1 %to &noe;
%let filename = %bquote(%sysfunc(dread(&did, &i)));
%let fid = %sysfunc(mopen(&did, &filename));
%if &fid > 0 %then %do;
%put &=path &=localpath &=_path &=filename;
ods package(newzip) add file="&_path\&filename" path="&localpath";
%end;
%else %do;
%if &localpath = %then
%readCatalog(&path, &filename);
%else
%readCatalog(&path, &localpath\&filename);
%end;
%end;
%let rc=%sysfunc(dclose(&did));
%mend readCatalog;
%macro createZIP(path, archive_name, archive_path);
%put *** Creating an archive (&archive_path\&archive_name) ***;
ods package(newzip) open nopf;
%readCatalog(&path)
ods package(newzip) publish archive properties(
archive_name="&archive_name"
archive_path="&archive_path"
);
ods package(newzip) close;
%mend createZIP;
%createZIP(C:\temp, test.zip, C:\temp2)
Regards
B
Hi Reeza,
Thanks again, but this is too generic for a very specific issue.
I've found the solution fortunately. Filling in the 3 parameters below does the trick.
At first it didn't work because I put the extension between quotes.............
Rgds
B
%let ext = EGP;
%let namezip = test.zip;
%let zippath = c:\temp;
%macro checkzipexistsdel (zipfile);
%if %sysfunc(fileexist(&zipfile.)) %then %do;
x del "&zipfile.";
%end;
%else %do ;
%put : Does not exist;
%end;
%mend;
%checkzipexistsdel (&zippath.\&namezip.);
%let n=0;
%macro readCatalog(path, localpath);
%local rc _path filrf did noe filename fid i;
%if %superq(localpath) = %then
%let _path=%superq(path);
%else
%let _path=%superq(path)\%superq(localpath);
%let n=%eval(&n+1);
%let filrf=DIR&n;
%let rc = %sysfunc(filename(filrf, %superq(_path)));
%let did = %sysfunc(dopen(&filrf));
%let noe = %sysfunc(dnum(&did));
%do i = 1 %to &noe;
%let filename = %bquote(%sysfunc(dread(&did, &i)));
%let fid = %sysfunc(mopen(&did, &filename));
%if &fid > 0 %then %do;
%put &=path &=localpath &=_path &=filename;
/*Run the zip process for a specific extension*/
%if %sysfunc(strip(%scan(%upcase(%superq(filename)),-1, '.'))) = &ext. %then %do;
ods package(newzip) add file="%superq(_path)\%superq(filename)" path="%superq(localpath)";
%put DE FILE IS %superq(filename);
%end;
%end;
%else %do;
%if %superq(localpath) = %then
%readCatalog(%superq(path), %superq(filename));
%else
%readCatalog(%superq(path), %superq(localpath)\%superq(filename));
%end;
%end;
%let rc=%sysfunc(dclose(&did));
%mend readCatalog;
%macro createZIP(path, archive_name, archive_path);
%put *** Creating an archive (&archive_path\&archive_name) ***;
ods package(newzip) open nopf;
%readCatalog(&path)
ods package(newzip) publish archive properties(
archive_name="&archive_name"
archive_path="&archive_path"
);
ods package(newzip) close;
%mend createZIP;
%createZIP(&zippath., &namezip., &zippath.);
The macro variable filename likely has the file with the extension. You can use %SCAN to get the extension and add an %IF then loop with that portion to include/exclude it.
Hi Reeza,
Thanks for the suggestion. I've tried that one, but without luck. I've even come to a point using this :
%if %sysfunc(strip(%scan(%upcase(%bquote(%sysfunc(dread(&did, &&i)))),-1, '.'))) = 'DOCX' %then %do;
%put This is a DOCX file;
%end;
The %put does not yield anything. I guess I do not know how to do this..
Rgds
B
Check the SAS 9.4 macro appendix and it has an example of how this is done, where it lists all file types of a specific extension. This code is very very similar so the same approach will work.
Hi Reeza,
Thanks again, but this is too generic for a very specific issue.
I've found the solution fortunately. Filling in the 3 parameters below does the trick.
At first it didn't work because I put the extension between quotes.............
Rgds
B
%let ext = EGP;
%let namezip = test.zip;
%let zippath = c:\temp;
%macro checkzipexistsdel (zipfile);
%if %sysfunc(fileexist(&zipfile.)) %then %do;
x del "&zipfile.";
%end;
%else %do ;
%put : Does not exist;
%end;
%mend;
%checkzipexistsdel (&zippath.\&namezip.);
%let n=0;
%macro readCatalog(path, localpath);
%local rc _path filrf did noe filename fid i;
%if %superq(localpath) = %then
%let _path=%superq(path);
%else
%let _path=%superq(path)\%superq(localpath);
%let n=%eval(&n+1);
%let filrf=DIR&n;
%let rc = %sysfunc(filename(filrf, %superq(_path)));
%let did = %sysfunc(dopen(&filrf));
%let noe = %sysfunc(dnum(&did));
%do i = 1 %to &noe;
%let filename = %bquote(%sysfunc(dread(&did, &i)));
%let fid = %sysfunc(mopen(&did, &filename));
%if &fid > 0 %then %do;
%put &=path &=localpath &=_path &=filename;
/*Run the zip process for a specific extension*/
%if %sysfunc(strip(%scan(%upcase(%superq(filename)),-1, '.'))) = &ext. %then %do;
ods package(newzip) add file="%superq(_path)\%superq(filename)" path="%superq(localpath)";
%put DE FILE IS %superq(filename);
%end;
%end;
%else %do;
%if %superq(localpath) = %then
%readCatalog(%superq(path), %superq(filename));
%else
%readCatalog(%superq(path), %superq(localpath)\%superq(filename));
%end;
%end;
%let rc=%sysfunc(dclose(&did));
%mend readCatalog;
%macro createZIP(path, archive_name, archive_path);
%put *** Creating an archive (&archive_path\&archive_name) ***;
ods package(newzip) open nopf;
%readCatalog(&path)
ods package(newzip) publish archive properties(
archive_name="&archive_name"
archive_path="&archive_path"
);
ods package(newzip) close;
%mend createZIP;
%createZIP(&zippath., &namezip., &zippath.);
@Billybob73 wrote:
Hi Reeza,
Thanks again, but this is too generic for a very specific issue.
B
Yet the code looks almost identical:
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do; %put &dir\&name; %end;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.