3

I've written a script which gets the subdirectories of a given (source) directory, and then checks for the existence of a matching subdirectory in another (destination) directory. If it exists in the destination, then it copies the contents of the subdirectory in to the destination. Here's what I have so far:

$src = read-host "Enter Source Dir"
$dst = read-host "Enter Destination Dir"
$log = read-host "Enter Log File Name"
foreach ($folder in Get-ChildItem -Path $src)
{
if ( -Not (Test-Path "$dst\$folder")) {} 
else {start-process robocopy.exe -ArgumentList "$src\$folder $dst\$folder /r:3 /w:5 /e /COPYALL /LOG+:D:\Temp\DataMigration\$log.log /zb /np /nc"}
}

The whole thing works quite nicely, unless the variables $src or $dst have a space within them. I also want to add a /XD Application Data argument to the argument list for robocopy, but the space in the name application data also causes errors.

Can anyone clear up how I can allow spaces where I need them as spaces rather than breaks in the script?

Thanks!

B

3 Answers 3

3

It looks like you should just wrap source and destination paths with a doubleqoutes and as soon as you need to use doublequotes inside another ones, you should escape them (inner doublequotes). There are several ways to do it. One of them is using backtick (`):

start-process robocopy.exe -ArgumentList "`"$src\$folder`" `"$dst\$folder`" /r:3 /w:5 /e /COPYALL /LOG+:D:\Temp\DataMigration\$log.log /zb /np /nc"
Sign up to request clarification or add additional context in comments.

1 Comment

Worked a treat including with the /XD flag for robocopy! Thanks for the quick reply, much appreciated!
2

Why are you using Start-Process for this? You can call robocopy directly from PowerShell:

robocopy.exe $src\$folder $dst\$folder /r:3 /w:5 /e /COPYALL /LOG+:D:\Temp\DataMigration\$log.log /zb /np /nc

1 Comment

I assumed calling it from powershell directly would make it pause until that copy op had completed before moving on to the next - but never checked to be honest as I had based my script above on a couple of other "standard" scripts I use. I'll bear your suggesting in mind for future though, thanks Keith!
2

Correct form of -ArgumentList will look like this array:

Start-Process -Path 'robocopy.exe' -ArgumentList @(
            $(Join-Path -Path $src -ChildPath $folder),
            $(Join-Path -Path $dst -ChildPath $folder),
            '/r:3',
            '/w:5',
            '/e',
            '/COPYALL',
            '/LOG+:D:\Temp\DataMigration\$log.log',
            '/zb',
            '/np',
            '/nc')

So each element of array will be a separate argument, and will not be divided into parts in arguments parameter in robocopy.exe

And it is more preferrable to use $(Join-Path -Path $dst -ChildPath $folder) istead of "$dst\$folder"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.