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?
 
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
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.