Copying User Data - Copy files where source folder <user>names differ from destination

support01
support01 used Ask the Experts™
on
I regularly need to copy user data from a long list of user paths for example:
from d:\usershares\Joe Bloggs to \\server\users\Co-JBloggs
from d:\usershares\John Doe to \\server\users\Co-JDoe
etc. etc...
(the folder names don't necessarily follow a convention.)

- I have a table with lists of source and destination folders.
- All user subfolders fall under the same top level folder.
- I do not need or want to copy permissions.
- I would prefer to achieve this using a proprietary Microsoft tool like Robocopy which I usually use for similar jobs. I cannot see a way to do this without creating separate jobs files for each user. I have a list of 80 names to copy on this occasion and would prefer not to have 80 separate jobs running concurrently.

Does anyone know a way to cleanly achieve this?

Examples would be excellent, if you care to include.

Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You won't get around the 80 different copies, but the good news is, you can just pack it into a loop, one after another.
Assuming you have a tab-delimited text file with <Source folder><Tab><Target Folder>, all you need is the following script.
If the delim differs, change it in line 7.
The script is currently in test mode and will only display the folders it would normally copy.
Remove the /L ("list only") from the RCOptions variable to copy for real.
Remove  /nfl (no file list), /ndl (no directory list) /njs (no job summary) as required, and add whatever other robocopy options you need.
@echo off
setlocal enabledelayedexpansion

set InputFile=C:\Temp\list.txt
set RCOptions=/e /r:0 /L /nfl /ndl /njs
REM Set the delim char between 'delim=' and the closing '"'
for /f "tokens=1,2 delims=	" %%a in ('type "%InputFile%"') do (
	set Source=%%~a
	set Target=%%~b
	if "!Source:~-1!"=="\" set Source=!Source!\
	if "!Target:~-1!"=="\" set Target=!Target!\
	robocopy.exe "!Source!" "!Target!" %RCOptions%
)

Open in new window

Author

Commented:
Excellent answer - just what i was looking for. Thank you for a real speedy response!
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
I think these two lines:

      if "!Source:~-1!"=="\" set Source=!Source!\
      if "!Target:~-1!"=="\" set Target=!Target!\


should be:

      if NOT "!Source:~-1!"=="\" set Source=!Source!\
      if NOT "!Target:~-1!"=="\" set Target=!Target!\


yes?

~bp
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Nope.
If a folder argument for robocopy is enclosed in quotes, and the last character of the folder string happens to be a backslash, then this backslash will escape the double quote, and robocopy will obviously not find the designated folder.
So if the last character happens to be a backslash, it needs to be escaped with an additional backslash.
And you can't just remove a trailing backslash, because you might encounter a root drive, in which case the copy will use D:<current working directory in D> when actually C:\ was meant. Shouldn't happen in this case, but I prefer not to rely on that.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Got it.

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial