如何解决Powershell转义特殊字符
我正在尝试对一系列文件执行Copy-Item
,并且注意到包含[
和]
的文件没有被复制,但是我没有收到任何错误。
我正在使用以下功能:
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"
}
}
}
每次运行时,它都会打印Copying: C:\Users\jesse_b\tmp\foo[1].txt to: C:\Users\jesse_b\tmp\test\foo[1].txt
,并且不会显示任何错误,但是文件实际上不会复制。
我尝试用双引号将变量名引起来,但这没有什么不同。
当我在终端上手动复制带有[
和]
的文件时,只要转义这些字符,它就可以工作。我必须做些什么才能将变量视为文字字符串?
解决方法
奇怪的是,这种方式有效,但是根据我自己的经验,我看到了类似的结果。
使用-LiteralPath:
Copy-Item -LiteralPath $f.fullname -Destination "$destFile"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。