kristof
kristof

Reputation: 53834

Copying folders using SAS X command: issues with quotations and special characters

I am trying to copy a set of folders using sas x command. The folders contain a mix of files of various types.

Let's assume the following file structure

D:\tmp\Src - directory with source folders
D:\tmp\Dest - destination directory

within the source directory I am interested in a subset of folders with names Data* eg: Data 1, Data 2 etc.

Defining source and destination main directories as macro variables:

%let srcLoc=D:\tmp\Src;
%let destLoc=D:\tmp\Dest;

we can run

x xcopy "&SrcLoc.\Data 1" "&destLoc.\Data 1" /q /i /y;
x xcopy "&SrcLoc.\Data 2" "&destLoc.\Data 2" /q /i /y;
/*etc */

And all works fine.

Intended modification

As with time, new folders will be added to source directory there is a need to write a universal code that would process all the folders that conform to naming Data*.

The following performs the task required task when executed directly within windows command line

FOR /F "tokens=*"  %G IN ('dir /B /A:D "D:\tmp\Src\Data*"') DO xcopy "D:\tmp\Src\%G" "D:\tmp\Dest\%G" /q /i /y

Issue:

When attempting to execute the same from within SAS:

%let srcLoc=D:\tmp\Src;
%let destLoc=D:\tmp\Dest;

/* just to test resolving of macro variables */
%put FOR /F "tokens=*"  %G IN ('dir /B /A:D "&srcLoc.\Data*"') DO xcopy "&srcLoc.\%G" "&destLoc.\%G" /q /i /y;

/* the actual x-command */
x FOR /F "tokens=*"  %G IN ('dir /B /A:D "&srcLoc.\Data*"') DO xcopy "&srcLoc.\%G" "&destLoc.\%G" /q /i /y;

There are the following issues:

As you can see the &srcLoc. did not resolve to a value as it is enclosed with single quotation marks needed to call ('dir /B /A:D "&srcLoc.\Data*"')

Question:

How to ensure that %G is not treated as macro and also ensure that &srcLoc. resolves to the actual value?

Also is there a better way to accomplish the above task within sas?

Upvotes: 1

Views: 1146

Answers (1)

user667489
user667489

Reputation: 9569

There's probably a better method, but for now I suggest you try one of the following:

Quote each problematic character individually

x FOR /F "tokens=*"  %nrstr(%)G IN (%bquote(')dir /B /A:D "&srcLoc.\Data*"%bquote(')) DO xcopy "&srcLoc.\%nrstr(%)G" "&destLoc.\%nrstr(%)G" /q /i /y;

Quote everything using masking % characters within a single %str

x %str(FOR /F "tokens=*"  %%G IN (%'dir /B /A:D "&srcLoc.\Data*"%') DO xcopy "&srcLoc.\%%G" "&destLoc.\%%G" /q /i /y);

Upvotes: 1

Related Questions