DATA Step, Macro, Functions and more

I want to copy all the directories within a folder to another folder

Reply
Contributor
Posts: 35

I want to copy all the directories within a folder to another folder

 

Hello 

 

I want to copy all the files from one folder to another folder 

for ex-C:\old folder

 

I want all the the  folder and file to be copied from old folder to new folder in the C drive

 

I am using below code 

 

let source=c:\old folder ;

%let target=c:\new folder ;

 

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;

 

 

the problem that I am facing is that 

 

a) if there is folder called  great and inside it has file new.txt  in C;\old folder then it copies in new path as new.txt but it can not creates  new.txt in a folder called great  

 

b) If I run my above program again it does not replace the file with the new one

 

please let em know how can I acheive ths 

 

 

Super User
Posts: 6,963

Re: I want to copy all the directories within a folder to another folder

Since you have commandline access (XCMD) anyway, use xcopy. Much easier.

And your problem b) is caused by the condition in your proc sql, which explicitly excludes files that already exist in target.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 35

Re: I want to copy all the directories within a folder to another folder

How can I do this thing through XCOPY .could you help me out ?

Super User
Super User
Posts: 7,413

Re: I want to copy all the directories within a folder to another folder

In SAS:

x 'copy "<path>" "<topath>"';

It is using the Operating System copy command.

Do bear in mind per my previous post, if the folders exist, you dont have access, something is open within them and any other number of things the process will fail - hence why I again suggest you fix your process rather than try to use a third party tool to do this.

 

Super User
Posts: 6,963

Re: I want to copy all the directories within a folder to another folder


Rohit12 wrote:

How can I do this thing through XCOPY .could you help me out ?


Gosh, you young padawans are so helpless at times!

Google for "Windows xcopy reference", and follow the links. The first one will be the MS reference page from Windows XP with lots of examples; I take it that not much has changed since then.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,413

Re: I want to copy all the directories within a folder to another folder

The question arises to me, why?  SAS is not the best tool for operating system management, it can do it, but if your doing that in a third party tool questions should be jumping to mind as to why.  Why those folders, do they need to be empty?  If they are duplicating, again why, if its datasets, you can do that in datastep, if its other files what else is going wrong to make you waant to copy them etc.

If you just need a fixed structure, you would be bettter off writing a small batch file to populate it.

Ask a Question
Discussion stats
  • 5 replies
  • 106 views
  • 1 like
  • 3 in conversation