Help using Base SAS procedures

Help with copying the files from one folder to another

Reply
Frequent Contributor
Posts: 124

Help with copying the files from one folder to another

I have folder called c:\Test2013  which has files Test112013.xls, Test122013.xls,  Test012014.xls, Test022014.xls, Test032014.xls, Test042014.xls  (each month files will be added).

I want to copy the above files to c:\Test2014 when I run my code every month.  If the files are already there in c:\Test2014 nothing will be copied.

Below is my code I started. Please let me know what can I do to make it work.

options xsync noxwait nosymbolgen nomlogic;

filename DIRLIST1 pipe 'dir "c:\Test2013\test*.*" ';              

                                                            

data dirlist1;                                              

infile dirlist1 lrecl=200 truncover;                         

input line $200.;                                           

if input(substr(line,1,10), ?? mmddyy10.) = . then delete;  

length file_name $ 100 ;   

file_name=scan(line,-1," ");   

keep file_name;  

run;     

  

filename DIRLIST2 pipe 'dir "c:\Test2014\test*.*" ';              

                                                            

data dirlist2;                                              

infile dirlist2 lrecl=200 truncover;                         

input line $200.;                                           

if input(substr(line,1,10), ?? mmddyy10.) = . then delete;  

length file_name $ 100 ;   

file_name=scan(line,-1," ");

keep file_name;  

run;     

proc sort data = dirlist1;

  by file_name;

run;

proc sort data = dirlist2;

  by file_name;

run;

data final;

  merge dirlist1(in=a) dirlist2(in=b);

  if a and not b;

  by file_name;

run;

data final;

  set final end=eof;

  count+1;

call symput('read1' ,left(trim(file_name)));  

if eof then call symputx('max_cnt',count);      

run;

%put &max_cnt.;

%put &read1.;

%macro file_copy; 

data _null_;

  %do i=1 %to &max_cnt;

   x "copy c:\Test2013\&read1.  c:\Test2014\&read1.";

%end;

run;

%mend;

%file_copy;

Frequent Contributor
Posts: 124

Re: Help with copying the files from one folder to another

I am able to copy the files from one folder to another folder:

Below is the code with changes:

options missing = ' ' xsync noxwait nosymbolgen nomlogic;

filename DIRLIST1 pipe 'dir "c:\Test2013\test*.*" ';              

                                                            

data dirlist1;                                              

infile dirlist1 lrecl=200 truncover;                         

input line $200.;                                           

if input(substr(line,1,10), ?? mmddyy10.) = . then delete;  

length file_name $ 100 ;   

file_name=scan(line,-1," ");   

keep file_name;  

run;     

  

filename DIRLIST2 pipe 'dir "c:\Test2014\test*.*" ';              

                                                            

data dirlist2;                                              

infile dirlist2 lrecl=200 truncover;                         

input line $200.;                                           

if input(substr(line,1,10), ?? mmddyy10.) = . then delete;  

length file_name $ 100 ;   

file_name=scan(line,-1," ");

keep file_name;  

run;     

proc sort data = dirlist1;

  by file_name;

run;

proc sort data = dirlist2;

  by file_name;

run;

data final;

  merge dirlist1(in=a) dirlist2(in=b);

  if a and not b;

  by file_name;

run;

data final;

  set final end=eof;

  count+1;

call symput('read1' || strip(put(count,8.)),left(trim(file_name)));  

if eof then call symputx('max_cnt',count);                       

run;

%put &max_cnt.;

%macro file_copy; 

data _null_;

 

%do i=1 %to &max_cnt;

   x "copy c:\Test2013\&&read1&i  c:\Test2014\&&read1&i";

%end;

run;

%mend;

%file_copy;

Super User
Super User
Posts: 6,498

Re: Help with copying the files from one folder to another

You are making it too complex.  Use the /B option of the DIR command so that you only need to read the file names.

Since you are using DOS you need to protect against embedded spaces in the path or filename. Also need to make the name match case insensitive.

%let source=c:\downloads\test1;

%let target=c:\downloads\test2;

data source ;

  infile "dir /b ""&source\"" " pipe truncover;

  input fname $256. ;

run;

data target ;

  infile "dir /b ""&target\"" " pipe truncover;

  input fname $256. ;

run;

proc sql noprint ;

  create table newfiles as

    select * from source

    where not (upcase(fname) in (select upcase(fname) from target ) )

  ;

quit;

data _null_;

   set newfiles ;

  cmd = catx(' ','copy',quote(catx('\',"&source",fname)),quote("&target"));

   infile cmd pipe filevar=cmd end=eof ;

   do while (not eof);

     input;

     put _infile_;

   end;

run;

Frequent Contributor
Posts: 124

Re: Help with copying the files from one folder to another

Thanks Tom...

Valued Guide
Posts: 3,208

Re: Help with copying the files from one folder to another

But why not use the OS commands? These kind of questions are very common at the OS level.

Knowing this how eays it can be, it is  also a threat for those people having a job doing just this kind of tricks at the OS level.   

There is a move aside copy command but also a xcopy (windows), that one is followed by robocopy Robocopy (TechNet microsoft) with /xct  option.
Robocopy is often present at Windows machines. Try that one in a dos-box.
When you would use a Unix (Linux) environment there are a lot of tools present by default also.

---->-- ja karman --<-----
Frequent Contributor
Posts: 90

Re: Help with copying the files from one folder to another

[ Edited ]

Hello all,

I am wondering if in my case I am doing 100K+ file copies but wanted to try and do it all in SAS if possible right now I am doing a system call to a batch file using robocopy.   From my batch file:

rem ...

rem define source, destination, and etc.
set MySource=F:\
set MyDestination=E:\FSS\F\
set MyLogFile=E:\FSS\F\Robocopy.log.txt
set MyExclude1=F:\E\
set MyExclude2=F:\Software\
set MyExclude3=E:\FSS\E\
set MyExclude4=%MyDestination%
set MyExclude5=E:\FSS\
echo "%MyLogFile%"
%MySource1%
cd /d %MyDestination% || goto :error
ROBOCOPY %MySource% %MyDestination% /E /xd %MyExclude1% %MyExclude2% %MyExclude3% %MyExclude4% %MyExclude5% /log:%MyLogFile% || goto :error1
echo "Done with robocopy %MySource% to %MyDestination%"
goto :skip1
:error1
echo "There was an error with robocopy however it did try to executed. Check the log: %MyLogFile%"
rem 'because robocopy was attempted however successful it was still run folder trimming'
:skip1

rem ...

 

 

This test case works but is just a fragment of my copy...

CALL SYSTEM ("
ROBOCOPY F:\Keith\ E:\FSS\FKeith\
 /E /xd F:\E\ F:\Software\ E:\FSS\FKeith\ E:\FSS\
 /log:E:\FSS\FKeith\Robocopy.log.txt
");

Has anyone done any macro work making it possible to put macro vars into a system call?   ...or is there a way to not just copy one dir to another but robo type copy in SAS?  TIA.

Ask a Question
Discussion stats
  • 5 replies
  • 2176 views
  • 2 likes
  • 4 in conversation