DATA Step, Macro, Functions and more

zip files from a specific type in a folder

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

zip files from a specific type in a folder

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


Accepted Solutions
Solution
‎02-20-2018 03:46 PM
Contributor
Posts: 55

Re: zip files from a specific type in a folder

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.);

View solution in original post


All Replies
Super User
Posts: 23,980

Re: zip files from a specific type in a folder

Posted in reply to Billybob73

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.

 

Contributor
Posts: 55

Re: zip files from a specific type in a folder

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

 

 

 

Super User
Posts: 23,980

Re: zip files from a specific type in a folder

Posted in reply to Billybob73

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. 

Solution
‎02-20-2018 03:46 PM
Contributor
Posts: 55

Re: zip files from a specific type in a folder

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.);

Super User
Posts: 23,980

Re: zip files from a specific type in a folder

Posted in reply to Billybob73

Billybob73 wrote:

Hi Reeza,

Thanks again, but this is too generic for a very specific issue.

 

B

 

Yet the code looks almost identical:

 

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&docset...

 

      %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
        %put &dir\&name;
      %end;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 137 views
  • 0 likes
  • 2 in conversation