Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

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

Avatar of oBdA
oBdA

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

Avatar of WeTi

ASKER

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...
Avatar of WeTi

ASKER

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
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

Avatar of WeTi

ASKER

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

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

ASKER

Works now, great support lord oBdA :)
Avatar of WeTi

ASKER

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
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.
Avatar of WeTi

ASKER

Yes, it was a older version of the code. Thanks again.
Avatar of WeTi

ASKER

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?
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.