BookmarkSubscribeRSS Feed
xiong
Fluorite | Level 6

Hi,

I want to do the following

1. 'Folder1' Contains all the different files.

2. I want to copy the specific CSV file into 'Folder2';

3. Finally I want to Zip the 'Folder 2' and Create the 'Folder2.Zip' 

 

I  am stuck at step 3, I see the  'ODS PACKAGES' (https://blogs.sas.com/content/sasdummy/2014/01/28/create-zip-ods-package/) unfortunately, I am not able to succeed. I am using SAS9.4 Windows environment.

Any inputs greatly appreciated.

%let pathscr = %str(C:\folder1);
%let pathdes = %str(C:\folder2);
data _null_;
  length fref $8 fname $256 cmd $500;
  did = filename(fref,"&pathscr");
  did = dopen(fref);
  
  if did = 0 then do;
    put "ERROR: Could not open directory &pathscr";
    stop;
  end;
  
  do i = 1 to dnum(did);
    fname = dread(did,i);
    if find(FNAME,'.CSV') then do;
      /* Use Windows copy command with /B and /Y options to preserve timestamps */
      cmd = 'cmd.exe /c copy /B /Y "' || 
            catx('\', "&pathscr", fname) || 
            '" "' || 
            catx('\', "&pathdes", fname) || 
            '"';
      rc = system(cmd);
      
      if rc = 0 then
        put "Successfully copied: " fname;
      else
        put "ERROR copying: " fname " Return code: " rc;
    end;
  end;
  
  did = dclose(did);
  rc = filename(fref);

run

 

3 REPLIES 3
Tom
Super User Tom
Super User

Do you really need to make two copies of the files? One in the directory and the second copy in the ZIP file.  Or is it sufficient to just put the files into the ZIP file directly?

 

Since you seem to want to copy TEXT files you can probably just use a data step to do the whole thing.

%let sdir = c:\downloads ;
%let zipfile = c:\downloads\csv_copy.zip ;

data _null_;
  length fname memname $200;
  infile "&sdir\*.csv" filename=fname ;
  input;
  memname = scan(fname,-1,'/\');
  file "&zipfile" zip memvar=memname;
  put _infile_;
run;
xiong
Fluorite | Level 6

Hi Tom,

 

Thank you for your Response, I am OK either way.  I just created two copies, since I am not sure How to move the required files from 'Folder1' to 'Folder2. Zip' directly also I want to control the zip folder name ( keep it as copy). One question to your code. Does it remains the metadata attributes of the original file when copied ( like time stamps).  I will give it a try using your code.

Tom
Super User Tom
Super User

It will make new files, so the timestamp on the files should be when you ran the code.

 

If you want to retain the timestamp you could try using the list of filenames to generate the code to copy the files.  For such jobs I find it works much easier to use a data step to write the desired code to a file and then use %INCLUDE to run the generated code.  Then you can use the full power of the data step (and especially the PUT statement) to generate the code.

%let sdir=C:\downloads ;
%let zipfile=c:\csv_copy.zip;

filename code temp;
data _null_;
  file code;
  infile "dir /b &sdir\*.csv" pipe truncover;
  input member $200. ;
  length fname $300 ;
  fname=quote(catx('\',"&sdir",member),"'");
  put 'filename in ' fname ';' ;
  put "filename out zip '&zipfile' " member= :$quote. ';' ;
  put '%let rc=%sysfunc(fcopy(in,out));' ;
run;

%include code / source2 ;

Note if you cannot use the PIPE method then use some other method to find the list of CSV files.  Such as this macro:  https://github.com/sasutils/macros/blob/master/dirtree.sas

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 317 views
  • 1 like
  • 2 in conversation