DATA Step, Macro, Functions and more

Copy directories and sub directories dynamically

Reply
Contributor
Posts: 35

Copy directories and sub directories dynamically

%let source=c:\New folder;

%let target=c:\shubham;

data source ;

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

  input fname $256. ;

run;

data target ;

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

  input fname $256. ;

run;

 

data new;

   set source ;

   length cmd1 $100.;

  cmd1 = catx(' ','xcopy',quote(catx('\',"&source",fname)),quote("&target"));

  cmd= cat('X ',cmd1);

 

   infile cmd  filevar=cmd end=eof ;

   do while (not eof);

     input;

     put _infile_;

   end;

run;

 

Below is the error that comes up 

 

ERROR: Invalid physical name.

fname=file1.txt cmd1=xcopy "c:\New folder\file1.txt" "c:\shubham"

cmd=X xcopy "c:\New folder\file1.txt" "c:\shubham" eof=0 _ERROR_=1 _INFILE_=  _N_=1

NOTE: The SAS System stopped processing this step because of errors.

NOTE: There were 1 observations read from the data set WORK.SOURCE.

WARNING: The data set WORK.NEW may be incomplete.  When this step was stopped there were 0

         observations and 2 variables.

WARNING: Data set WORK.NEW was not replaced because this step was stopped.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

 

 

My goal is to copy all directories and subdirectories dynamically 

 

how can I do that 

Super User
Super User
Posts: 7,413

Re: Copy directories and sub directories dynamically

This line:

  infile cmd...

 

As you can see from the error, CMD variable contains:

X xcopy "c:\New folder\file1.txt" "c:\shubham"

 

Which is not valid SAS syntax.  Infile requires a path/filename, or fileref.

 

What is it your copying, what is the process?  As I mentioned before, programs which create/copy folders/files should never really be needed.  You process should take care of that - i.e. follow an operating procedure, be inline with IT requirements, permissions etc.  Simply copying an pasting directories/files willy nilly will flag you up to IT.

Contributor
Posts: 35

Re: Copy directories and sub directories dynamically

what I want is that I want to copy all the directories and subdirectories through sas 

 

for ex-I wan to copy from old folder  which contain file1.txt and file2.txt  path- c:\old  folder  to a new path c:\new folder

 

how can I do this by SAS program

Super User
Super User
Posts: 7,413

Re: Copy directories and sub directories dynamically

I understand what you want, I don't understand why you want to do that.  Its never a good idea to multiple your file system, it sounds like there is some reasoning as to copying that, maybe thats a fixed copy and you don't want to alter it?  If so open it as read only and work with it in your work area - this is just an example.  

Contributor
Posts: 35

Re: Copy directories and sub directories dynamically

what i want is that whenver  I run SAS program it should copy file from one  directories to another

how caN i acheive that using SAS or any other  way 

 

please help me

 

 

 

 

Super User
Posts: 6,955

Re: Copy directories and sub directories dynamically

filename oscmd pipe "xcopy c:\source\* c:\target /s /y 2>&1";

data _null_;
infile oscmd;
input;
put _infile_;
run;

will copy all files in c:\source to c:\target, including all subdirectories. /y prevents prompting when a file already exists in the target.

The filename pipe and data step are used to catch output from the command and write it to the SAS log; if you don't need that, you can issue the xcopy command in a X statement.

I recommend doing the filename/data step method at least while testing.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,777

Re: Copy directories and sub directories dynamically


Rohit12 wrote:

%let source=c:\New folder;

%let target=c:\shubham;

data source ;

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

  input fname $256. ;

run;

data target ;

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

  input fname $256. ;

run;

 

data new;

   set source ;

   length cmd1 $100.;

  cmd1 = catx(' ','xcopy',quote(catx('\',"&source",fname)),quote("&target"));

  cmd= cat('X ',cmd1);

 

   infile cmd  filevar=cmd end=eof ;

   do while (not eof);

     input;

     put _infile_;

   end;

run;

 

Below is the error that comes up 

 

ERROR: Invalid physical name.

fname=file1.txt cmd1=xcopy "c:\New folder\file1.txt" "c:\shubham"

cmd=X xcopy "c:\New folder\file1.txt" "c:\shubham" eof=0 _ERROR_=1 _INFILE_=  _N_=1

NOTE: The SAS System stopped processing this step because of errors.

NOTE: There were 1 observations read from the data set WORK.SOURCE.

WARNING: The data set WORK.NEW may be incomplete.  When this step was stopped there were 0

         observations and 2 variables.

WARNING: Data set WORK.NEW was not replaced because this step was stopped.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

 

 

My goal is to copy all directories and subdirectories dynamically 

 

how can I do that 


Looks to me like you want to execute the XCOPY with PIPE not X command.  See if this works...

 

data new;
   set source ;
   length cmd1 $100.;
  cmd1 = catx(' ','xcopy',quote(catx('\',"&source",fname)),quote("&target"));
 
   infile dummy pipe filevar=cmd1 end=eof ;
   do while (not eof);
     input;
     put _infile_;
   end;
run;

Notice the change to the INFILE statement.

Contributor
Posts: 35

Re: Copy directories and sub directories dynamically

No it does not works

 

below is the note  that is coming in  that's why it is not copying it 

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\file1.txt" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\file2.txt" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\great" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\harnem.zip" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\new file.txt" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: 0 records were read from the infile DUMMY.

NOTE: 0 records were read from the infile DUMMY.

NOTE: 0 records were read from the infile DUMMY.

NOTE: 0 records were read from the infile DUMMY.

NOTE: 0 records were read from the infile DUMMY.

 

let  me know how can i correct this 

Super User
Posts: 6,955

Re: Copy directories and sub directories dynamically


Rohit12 wrote:

No it does not works

 

below is the note  that is coming in  that's why it is not copying it 

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\file1.txt" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\file2.txt" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\great" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\harnem.zip" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: The infile DUMMY is:

      Unnamed Pipe Access Device,

      PROCESS=xcopy "c:\New folder\new file.txt" "c:\shubham",

      RECFM=V,LRECL=256

 

NOTE: 0 records were read from the infile DUMMY.

NOTE: 0 records were read from the infile DUMMY.

NOTE: 0 records were read from the infile DUMMY.

NOTE: 0 records were read from the infile DUMMY.

NOTE: 0 records were read from the infile DUMMY.

 

let  me know how can i correct this 


The fact that xcopy does not give you any output usually means it worked as expected. Look in the target directory if the files are there.

If they're not there, run xcopy from the commandline outside of SAS and see if you get error messages.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,955

Re: Copy directories and sub directories dynamically

And with xcopy, you do not have to iterate through a directory structure, as xcopy is specifically designed to do that on its own (different form the simple copy).

Have you already tried this:

filename oscmd pipe 'xcopy "c:\New folder\*" "c:\shubham /s /y 2>&1';

data _null_;
infile oscmd;
input;
put _infile_;
run;

?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor ckx
Contributor
Posts: 54

Re: Copy directories and sub directories dynamically

I've tried Kurt Bremser's solution and it doesn't work for me. I alose get a note

 

 

NOTE: 0 records were read from the infile OSCMD.

The destination folder remains empty. This is odd, the PIPE method works fine with other Windows shell commands such as copy, move, del.

 

A solution that does work is to sue robocopy.

 

robocopy "<source>" "<dest>" * /is

Robocopy (Robust File Copy for Windows) does have a quirk, you can't use a double period to go up one level in your program path. It also produces more output but that's not necessarily a problem. I hadn't heard of it until today, documentation is at https://technet.microsoft.com/nl-nl/library/cc733145(v=ws.10).aspx.

 

What interests me is why the xcopy command isn't being executed. Does anyone know?

Super User
Posts: 6,955

Re: Copy directories and sub directories dynamically

Test the xcopy from the commandline before running it from SAS. Basically, the 2>&1 at the end of the command should redirect error messages to the pipe, though, so if something is amiss SAS should catch it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor ckx
Contributor
Posts: 54

Re: Copy directories and sub directories dynamically

Hi Kurt,

 

The xcopy command does work from a command line or if modified to a robocopy command. What's telling is the message "0 records were read". The command isn't generating an error, it's being completely ignored. But the equivalent robocopy command does work!

 

Does the xcopy command work for you? If so, what version of SAS are you using and on which platform? I'm using SAS 9.4M1 on a Windows 2008 server. I'm using Enterprise Guide but with the "xcmd" option enabled (by default, Enterprise Guide won't let you execute shell commands but our SAS administrator enabled this option).

 

 

Super User
Posts: 6,955

Re: Copy directories and sub directories dynamically

Wow. I just checked and couldn't believe it. While other things work as they should, xcopy obviously plays "dead" when run from SAS. I simply couldn't believe that such a simple utility would do that.

Even after making sure that no further input would be expected from me.

 

Thank $DEITY I don't have to put up with such crap. My SAS runs on UNIX.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 6,502

Re: Copy directories and sub directories dynamically

Can you use COPY to copy any of the files?

Perhaps it is a permission issue with the account that your SAS server is using?

 

http://stackoverflow.com/questions/24485086/xcopy-does-not-copy-all-files

 

Ask a Question
Discussion stats
  • 15 replies
  • 257 views
  • 1 like
  • 6 in conversation