BookmarkSubscribeRSS Feed
Rohit12
Obsidian | Level 7

 

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 

 

 

5 REPLIES 5
Kurt_Bremser
Super User

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.

Rohit12
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

Kurt_Bremser
Super User

@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.

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 5434 views
  • 1 like
  • 3 in conversation