Link to home
Start Free TrialLog in
Avatar of ASPDaddy
ASPDaddyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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:

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

hth

Steve
Avatar of ASPDaddy

ASKER

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
Avatar of oBdA
oBdA

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

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.  
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fine, please yourself, we're not mind readers.
Perfect, thanks.
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 ...
<<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