Xcopy loop over user list in batch file

Is theer a way to set a variable for  a list of user folders and execute the xcopy within  a loop, rather than have to  list out each line as below.

Thanks

@Echo Off
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set Today=%%A%%B%%C
)
Set FromPath=\\myserver\folders\
Set ToPath=\\mynas\%Today%

xcopy /s /c /h /i /q /r /n /y  "%FromPath%Rita"   "%ToPath%"
xcopy /s /c /h /i /q /r /n /y  "%FromPath%Sue"   "%ToPath%"
xcopy /s /c /h /i /q /r /n /y  "%FromPath%Bob"    "%ToPath%"
...etc

ASPDaddyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
Sure.  You can read all the dirs under a path using a for command, e.g.

@Echo Off
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set Today=%%A%%B%%C
)
Set FromPath=\\myserver\folders\
Set ToPath=\\mynas\%Today%

For /D %%U in (%Frompath%\*) do (
  xcopy /s /c /h /i /q /r /n /y  "%FromPath%%%~nU"   "%ToPath%"
)

or just

For /D %%U in (%Frompath%\*) do xcopy /s /c /h /i /q /r /n /y  "%%~U"   "%ToPath%"

Be careful getting dates that way btw, different date formats and the like.  My article here shows some other ways, and also you can use WMIC command too:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

hth

Steve
0
ASPDaddyAuthor Commented:
I dont want to read all the dirs - or I would just set the from path higher :) I just want the code set a var for defined list of users;  users="rita,sue,bob" etc and execute the copy using it
0
oBdACommented:
Try this then; folders with spaces in it need to be enclosed in double quotes in FolderList:
@Echo Off
set FolderList=rita sue bob "john doe"
For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( 
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set Today=%%A%%B%%C
)
Set FromPath=\\myserver\folders\
Set ToPath=\\mynas\%Today%
for %%a in (%FolderList%) do (
  xcopy /s /c /h /i /q /r /n /y  "%FromPath%\%%~a"   "%ToPath%"
)

Open in new window

0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

ASPDaddyAuthor Commented:
Thats what I needed Thanks.  but its not creating the recreating the users folders with the names on - just dropping all the files in the deestination.  
0
oBdACommented:
@Echo Off
set FolderList=rita sue bob "john doe"
For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( 
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set Today=%%A%%B%%C
)
Set FromPath=\\myserver\folders
Set ToPath=\\mynas\%Today%
for %%a in (%FolderList%) do (
  xcopy /s /c /h /i /q /r /n /y  "%FromPath%\%%~a"   "%ToPath%\%%~a"
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
Fine, please yourself, we're not mind readers.
0
ASPDaddyAuthor Commented:
Perfect, thanks.
0
oBdACommented:
dragon-it,
ASPDaddy actually explicitly said "[...] set a variable for a list of user folders [...]".
Though admittedly in most of the cases this means that the list should be generated dynamically ...
0
Steve KnightIT ConsultancyCommented:
<<I dont want to read all the dirs - or I would just set the from path higher :) >>

Well it wouldn't have done the same.... it would have given you subdirs under yout "to path" for each user too.

If I'd have specified particular users in a variable I'd have been asked for it to read the directory structure...

Always best to read dynamically and exclude ones you don't want than the other way around otherwise you have to remember to go and put any new users in as you go along...

Anyway you've got your answer.

Steve

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Miscellaneous

From novice to tech pro — start learning today.