Reputation: 149
I'm trying to perform a Copy-Item
on a series of files and I'm noticing that files containing [
and ]
that are not being copied, however I'm not receiving any error.
I'm using the following function:
function fsync ($source,$target) {
$sourceFiles = @(Get-ChildItem -Path $source -Recurse | select -expand FullName)
foreach ($f in $sourceFiles) {
$destFile = $f.Replace($source,$target)
$exist = Test-Path $destFile
if ($exist -eq $false) {
Write-Host "Copying: " $f "to: " $destFile
Copy-Item -Path "$f" -Destination "$destFile"
} else {
Write-Host "File: " $f "Already exists"
}
}
}
Every time it's run it will print Copying: C:\Users\jesse_b\tmp\foo[1].txt to: C:\Users\jesse_b\tmp\test\foo[1].txt
, and display no error, but the file never actually copies.
I've tried double quoting the variable names but it didn't make a difference.
When I manually copy files with [
and ]
in them on the terminal it works as long as those characters are escaped. Is there something I must to do to have my variables treated as literal strings?
Upvotes: 0
Views: 430
Reputation: 3218
Weird that it works that way, but I've seen similar results in my own experience.
Use -LiteralPath:
Copy-Item -LiteralPath $f.fullname -Destination "$destFile"
Upvotes: 1