Robocopy multiple source folders to one destination

Dear expert

I would like to use robocopy to copy multiple folders to one destination.

I tried with powershell, but it didn't work what I mean by didn't work is the folder did not copy to destination.

$source1="\\mgmt\D$\Test\test1"
$source2="\\mgmt\D$\Test\test2"
$source3="\\mgmt\D$\Test\test3"
$dest="\\mgmt\d$\test1"

$what = @("/E")
$options = @("/R:0")

$cmdArgs1 = @("$source1","$dest",$what,$options)
robocopy @cmdArgs1
$cmdArgs2 = @("$source2","$dest",$what,$options)
robocopy @cmdArgs2
$cmdArgs3 = @("$source3","$dest",$what,$options)
robocopy @cmdArgs3

Open in new window

LVL 1
WeTiAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Sorry, had it all in the robocopy line at first, and then decided it would be easier to see what's happening when moving that into a separate line. Just a pair of brackets missing:
$sources = @(
	'\\mgmt\D$\Test\test1'
	'\\mgmt\D$\Test\test2'
	'\\mgmt\D$\Test\test3'
)
$dest = '\\mgmt\d$\test1'

$what = @('/E')
$options = @('/R:0', '/L')
ForEach ($source in $sources) {
	$target = Join-Path -Path $dest -ChildPath ([IO.Path]::GetFileName($source))
	& robocopy.exe $source $target $what $options
}

Open in new window

0
 
oBdACommented:
Access assumed, I don't see why that shouldn't run.
But in general, you should try to avoid duplicating code. And in PowerShell, external programs should be called using "&" and including the extension to make it clearer what's going on.
Try it like this:
$sources = @(
	'\\mgmt\D$\Test\test1'
	'\\mgmt\D$\Test\test2'
	'\\mgmt\D$\Test\test3'
)
$dest = '\\mgmt\d$\test1'

$what = @('/E')
$options = @('/R:0', '/L')
ForEach ($source in $sources) {
	& robocopy.exe $source $dest $what $options
}

Open in new window

0
 
WeTiAuthor Commented:
This is only a test so test1 and 2 and 3 folder is empty, that is why Im using /E, the deal is when I run that command, folder did not copy from d:\test\test1 2 3 to test1 folder...
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
WeTiAuthor Commented:
Ah I see the problem, the robocopy is trying the copy all files inside that folder to $dest, well I want it to copy the folder (Not files in it)test1 test2 test3 and copy to $dest
0
 
oBdACommented:
Then try it like that:
$sources = @(
	'\\mgmt\D$\Test\test1'
	'\\mgmt\D$\Test\test2'
	'\\mgmt\D$\Test\test3'
)
$dest = '\\mgmt\d$\test1'

$what = @('/E')
$options = @('/R:0', '/L')
ForEach ($source in $sources) {
	$target = Join-Path -Path $dest -ChildPath [IO.Path]::GetFileName($source)
	& robocopy.exe $source $target $what $options
}

Open in new window

0
 
WeTiAuthor Commented:
It doesn't understand the argument in the source var. I just want to copy those folder test1 test2 test3 with contents in it into $dest.

Join-Path : A positional parameter cannot be found that accepts argument '\\mgmt\D$\Test\test3'.
At line:11 char:12
+     $target = Join-Path -Path $dest -ChildPath [IO.Path]::GetFileName($source)
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Join-Path], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

Open in new window

Join-Path : A positional parameter cannot be found that accepts argument '\\mgmt\D$\Test\test2'.
At line:11 char:12
+     $target = Join-Path -Path $dest -ChildPath [IO.Path]::GetFileName($source)
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Join-Path], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

Open in new window

Join-Path : A positional parameter cannot be found that accepts argument '\\mgmt\D$\Test\test3'.
At line:11 char:12
+     $target = Join-Path -Path $dest -ChildPath [IO.Path]::GetFileName($source)
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Join-Path], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

Open in new window

0
 
WeTiAuthor Commented:
Works now, great support lord oBdA :)
0
 
WeTiAuthor Commented:
ok... but now it's another problem, I tried to add files in d:\test\test1\st.bak what script did was copy the folders and copy file under the d:\test1 (as root of the destination) not d:\test1\test1\st.bak. ForEach ($source in $sources) you basicly pick the file by file and put it in the destination folder, that was why the file inside the folder d:\test\test1\st.bak is inside the root of destination folder d:\test1\st.bak which is "wrong" result, I want it to be inside the d:\test1\test1\st.bak
0
 
oBdACommented:
Shouldn't happen; could it be you used an older version of the script, or seeing remnants of old attempts?
The script will run one separate robocopy command for each source folder in the $sources array.
From each of these folders, it will take the last name, and add it to the destination folder.
One possible issue just occurred to me: do not use trailing backspaces in the $sources array.
And just in case you haven't discovered it yourself yet: the script above is in test mode and will only display what it would normally copy.
Remove the /L option in line 9 to run it for real.
1
 
WeTiAuthor Commented:
Yes, it was a older version of the code. Thanks again.
0
 
WeTiAuthor Commented:
just another question, what happen if the robocopy detect there are a folder with same source folder name in destination folder? Will it just copy the different from source to destination?
0
 
oBdACommented:
It will just overwrite/add with the arguments you're using. robocopy will by default skip files that are already in the target folder (same date, same size).
Last writer wins; so if you always want the latest version of files with the same name, you need to add /XO (eXclude Older) to your options.
1
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.