Hey folks,
I'm working on an automated process at my job where we need to copy SQL backup files from a UNC path to a virtual machine. The tricky part is that some of these paths contain spaces, and I'm having a hard time getting Robocopy to handle them properly. The UNC path is stored in a variable called `$StagingDatabaseContainer`, which I get from a Powershell script argument, and this variable is always in single quotes. Most of my attempts to use Robocopy have failed because it sees the path with spaces as multiple arguments.
I've tried several methods using the call operator and Start-Process, but I always end up with the same problem. I also experimented with using `Resolve-Path`, but since I have to convert it back to a string for Robocopy, I still encounter the issues with spaces.
Has anyone found a reliable way to work with UNC paths that include spaces when using Robocopy? Thanks a bunch!
3 Answers
If you haven't seen it, there's a useful discussion over on Stack Overflow about handling paths with spaces in Robocopy. It covers a lot of common pitfalls, so it might help you out! Here's the link: https://stackoverflow.com/questions/12027987/how-to-copy-directories-with-spaces-in-the-name
I recommend sticking with the call operator for running Robocopy. You can use it like this:
```
& 'C:WindowsSystem32Robocopy.exe' $StagingDatabaseContainer C:dbbackup *.bak /np /r:3 /w:60 /log+:c:temprobocopy_dbs.log
```
Just don't forget to trim any extra quotes from your UNC path using `Trim("'")`. That should make it smooth sailing!
Robocopy and PowerShell should be able to manage quotes for you. What you might not realize is that if your variable has a trailing backslash, it'll break things. Make sure you clean up the end of your path first! Just trim that trailing backslash, then call Robocopy like this:
```
& 'C:WindowsSystem32Robocopy.exe' $StagingDatabaseContainer C:dbbackup *.bak /np /r:3 /w:60 /log+:c:temprobocopy_dbs.log
```
This way, you won't run into any issues with spaces, and PowerShell will handle the quoting!
Exactly! I found out the same thing. I was messing around with the quoting and the call operator, but really, it was just that pesky trailing backslash messing everything up.
That approach didn't solve my issue at first, but it led me to realize I had a trailing backslash in my UNC path. Once I trimmed that off, it worked perfectly!