PowerShell and Robocopy - filename incorrect when trying to pass variable folder as destination

0

I'm trying to use a PowerShell script running Robocopy* to back some files up to a newly-made directory:

$Timestamp = Get-Date -format ddMMyyyy

$DestFolder = "`"\\NASBOX\Archives\$Timestamp\`""
$SourceFolder = "`"\\DESKTOP\d$`""

ROBOCOPY $SourceFolder $DestFolder /COPYALL /B /R:10 /W:90 /LOG:$Timestamp.txt /FP /TEE

This gives me the following error:

2018/01/23 16:26:20 ERROR 123 (0x0000007B) Accessing Destination Directory \\NASBOX\Archives\23012018" \COPYALL \B \R:10 \W:90 \LOG:23012018.txt \FP \TEE\
The filename, directory name, or volume label syntax is incorrect.

I've tried a few different methods, including passing the arguments as an array. Every single thing I've tried results in the exact same error.

I roughly understand why this is happening, but despite ~two hours spent online I can't find a solution that works in my specific context.

Where am I going wrong?

* I tried using Copy-Item but there are some super long directory paths on this desktop's "D" drive.

powershell
robocopy
asked on Stack Overflow Jan 23, 2018 by turbonerd • edited Jan 23, 2018 by James C.

3 Answers

1

You don't need to try so hard with escaping of quotes in your variables. PowerShell handles most of this for you. This should be all you need to do:

$Timestamp = Get-Date -Format ddMMyyyy

$SourceFolder = "\\DESKTOP\d$"
$DestFolder = "\\NASBOX\Archives\$Timestamp"

ROBOCOPY $SourceFolder $DestFolder /COPYALL /B /R:10 /W:90 /LOG:$Timestamp.txt /FP /TEE

Note that the destination folder shouldn't include a trailing \.

TL;DR - It is not necessary to create strings with embedded " characters to pass to robocopy. Just put the variables on the robocopy command line and PowerShell will quote automatically when necessary.

answered on Stack Overflow Jan 23, 2018 by Bill_Stewart • edited Jan 25, 2018 by Bill_Stewart
1

The issue is the trailing slash in the path you are building:

"\\NASBOX\Archives\23012018\"

This slash is escaping the double quote for robocopy, it is seeing this path as including a quote symbol at the end :

\\NASBOX\Archives\23012018"

The error message shows this, but isn't very helpful! To fix the issue, simply remove the trailing slash from your path:

$DestFolder = "`"\\NASBOX\Archives\$Timestamp`""
answered on Stack Overflow Jan 23, 2018 by James C.
-1
Function Copy-File {
    [CmdletBinding()]   
    Param(
        [Parameter(Position=0)]
        [string]$source,
        [Parameter(Position=1)]
        [string]$dest,
        [Parameter(Position=2)]
        [string]$sourcefile,
        [Parameter(Position=3)]
        [ref]$RoboError
        )
    Write-Log -message "Copying $sourcefile from $source to $dest"
    $robotoday=(Get-Date).ToString('yyyyMMdd')
    $logfile = -join($env:systemdrive, '\logs\', $robotoday, '_robocopy.log')
    $what = @("$sourcefile",'/COPY:DAT', '/Z', '/E')
    $options = @("/R:1","/W:1","/TEE","/ETA","/LOG+:$logfile")
    $cmdArgs = @($source,$dest,$what,$options)
    robocopy @cmdArgs
    if ($lastexitcode -gt 7) {
        $RoboError.value=$TRUE
        Write-Log -level 'warn' -message "Robocopy function failed with error: $lastexitcode"
    }
} # End Copy-File

[bool]$RoboError=$FALSE
Copy-File -source $copysource -dest $copydestination -sourcefile '*' -RoboError([ref]$RoboError)
answered on Stack Overflow Jan 25, 2018 by psguest

User contributions licensed under CC BY-SA 3.0