DATA Step, Macro, Functions and more

how to copy multple files from one location to another where the date within the filename changes?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

how to copy multple files from one location to another where the date within the filename changes?

I need to copy mutiple files from one location to another where the date within the filename changes on a daily basis and I would need to copy a whole months worth at a time. There could also be other files with a different month within the date part that I don't want to copy.

 

Typical file names would be File_01May2016.zip, File_07May2016.zip, File_09May2016.zip, File_11May2016.zip, File_01Jun2016.zip. I would want to copy tghe four files containing the value May2016 (which I have set in a macro field called &MONTH) into a location where the directory is called 'MAY2016.

 

Once they have been copied to the correct location I need to unzip all the files within that new location.

 

Any advice on how to code this would be much appreciated.

 

Thanks

Louise


Accepted Solutions
Solution
‎06-14-2016 04:32 AM
Occasional Contributor
Posts: 5

Re: how to copy multple files from one location to another where the date within the filename change

Hi All,

 

Thanks to everyone that posted a suggestion - we have managed to solve the issue ourselves in the end but all suggestions much appreciated.

View solution in original post


All Replies
Super Contributor
Posts: 408

Re: how to copy multple files from one location to another where the date within the filename change

Hi Louise,

 

SAS can do a lot, and most of it is done very well indeed. But in this scenario SAS may not be the best tool. I would consider a script of some sort on the operating system (we would like to know which one anyways) as the way to go. That script could in turn be executed from SAS if you like but SAS would add very little benefit. And if copying is done by SAS then the unzipping would be a real challenge. 

 

Cheers,

- Jan.

Super User
Posts: 6,927

Re: how to copy multple files from one location to another where the date within the filename change

Look at this:

cd sourcelocation
DATE=$1
mkdir targetlocation/${DATE}
for FILE in *${DATE}*.zip;do
  cp $FILE targetlocation/${DATE}
  unzip targetlocation/${DATE}/${FILE}
done

This is a sample UNIX shell script that does what you want. Doing the same in SAS would be much more complicated.

Use the right tool for the job.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: how to copy multple files from one location to another where the date within the filename change

Thanks for the responses guys but I really need to do it in SAS as the final program needs to put into an automated production for a monthly report. I should be able to use the wzinzip and copy functions but it's just identifying the filenames that contain the month of May, or whatever it would be at the time of the scheduled run.

 

Thanks

Louise

Super User
Posts: 6,927

Re: how to copy multple files from one location to another where the date within the filename change

So which system is it? UNIX or WIndows?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: how to copy multple files from one location to another where the date within the filename change

The system we use is Windows

Super User
Posts: 6,927

Re: how to copy multple files from one location to another where the date within the filename change

Ok, let's try

Initialize:

%let sourcepath=where_your_files_are;
%let targetpath=where_you_want_them;
%let month=MAY2016;

Create a dataset with file names:

filename oscmd pipe "dir &sourcepath.\*&month.*.zip";

data filenames;
length fname $254;
infile oscmd;
input fname;
run;

Now act on that

data _null_;
set filenames;
if _n_ = 1 then call system("mkdir &targetpath.\&month");
call system("copy " !! fname !! " &targetpath.\&month");
fname = scan(fname,-1,'\');
call system("winzip32 -e &targetpath.\&month.\" !! fname);
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎06-14-2016 04:32 AM
Occasional Contributor
Posts: 5

Re: how to copy multple files from one location to another where the date within the filename change

Hi All,

 

Thanks to everyone that posted a suggestion - we have managed to solve the issue ourselves in the end but all suggestions much appreciated.

Super Contributor
Posts: 408

Re: how to copy multple files from one location to another where the date within the filename change

 Out of cusiosity and always looking for a learning opportunity, could you share your solution with us?

 

- Jan.

Occasional Contributor
Posts: 5

Re: how to copy multple files from one location to another where the date within the filename change

As requested:

/*set file locations from and to*/
%let statement_fm = &wr\XXXX\XXXX;
%let statement_to = &wr\XXXX\XXXX\&MONTH;
 
Data List_files (keep =  filevar);
 rc=FILENAME('stmnts',"&file_fm");
 did=DOPEN('stmnts');
 memcnt=DNUM(did);
 
 *count number of members  - including subfolders;
 DO i=1 TO memcnt;
  *for each member, test extension and store filename;
  filevar=(DREAD(did,i));
 
  IF LOWCASE(SCAN(filevar,-1,".")) EQ LOWCASE("zip") THEN
   OUTPUT;
 END;
 
 rc=DCLOSE(did);
 rc=FILENAME('stmnts');
Run;

proc sql;
 select
   filevar, count(*)
   into: file1-,: obs
 from work.list_files(where=(lowcase(filevar) like "%%&monthname.%"));
quit;

%macro copy_files;
 %do i=1 %to &obs.;
  %put &&file&i.;
  %sysexec move "&file_fm\&&file&i." "&file_to\&&file&i.";
 %end;
%mend;

%copy_files;

%macro unzip_files;
    %do i=1 %to &obs.;
     %put &&file&i.;
  %sysexec wzunzip "&file_to\&&file&i." "&file_to\" -d;
 %end;
%mend;

%unzip_files;

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 349 views
  • 0 likes
  • 3 in conversation