ASPDaddy
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\folder s\
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
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\folder
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
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
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%"
)
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Fine, please yourself, we're not mind readers.
ASKER
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 ...
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
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
@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\folder
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