如何处理带空格的目录?
How to handle directories with spaces?
我想将 PowerShell 与 robocopy
结合使用来移动大量文件。如果任何目录中都没有空格,则下面的代码有效。我如何改进它以处理带空格的目录?
$workingDirectory = Split-Path -Parent $PSCommandPath
$sourceDir = "$workingDirectory\source folder"
$targetDir = "$workingDirectory\target folder"
$logFile = "$workingDirectory\log.txt"
$options = "/copyall /b /is /r:5 /w:5 /log:$logFile"
Start-Process robocopy -args "$sourceDir $targetDir $options" -NoNewWindow -PassThru -Wait
在不知道具体错误的情况下很难说清楚。您可以在 $sourceDir
和 $targetDir
周围添加转义引号,以便 PowerShell 在启动新进程以执行 robocopy
.
时不会删除引号
Start-Process robocopy -args `"$sourceDir`", `"$targetDir`", $options -NoNewWindow -PassThru -Wait
根据 Ansgar Wiechers 的评论,我弄明白了。谢谢:-)
$workingDirectory = Split-Path -parent $PSCommandPath
$SourceDir = "$workingDirectory\source folder"
$targetDir = "$workingDirectory\target folder"
$logFile = "$workingDirectory\log.txt"
$options = "/copyall /b /is /r:5 /w:5 /log:$logFile"
& robocopy "$sourceDir" "$targetDir" /copyall /b /is /r:5 /w:5 "/log:$logFile"
好的,我在使用 Start-Process 时看到错误:
ERROR : Invalid Parameter #3 : "...\target"
然而,运行 robocopy 直接不需要特殊引用。请注意,/copyall 需要管理员权限。
robocopy $sourcedir $targetdir /copyall /b /is /r:5 /w:5 /log:$logFile
我想将 PowerShell 与 robocopy
结合使用来移动大量文件。如果任何目录中都没有空格,则下面的代码有效。我如何改进它以处理带空格的目录?
$workingDirectory = Split-Path -Parent $PSCommandPath
$sourceDir = "$workingDirectory\source folder"
$targetDir = "$workingDirectory\target folder"
$logFile = "$workingDirectory\log.txt"
$options = "/copyall /b /is /r:5 /w:5 /log:$logFile"
Start-Process robocopy -args "$sourceDir $targetDir $options" -NoNewWindow -PassThru -Wait
在不知道具体错误的情况下很难说清楚。您可以在 $sourceDir
和 $targetDir
周围添加转义引号,以便 PowerShell 在启动新进程以执行 robocopy
.
Start-Process robocopy -args `"$sourceDir`", `"$targetDir`", $options -NoNewWindow -PassThru -Wait
根据 Ansgar Wiechers 的评论,我弄明白了。谢谢:-)
$workingDirectory = Split-Path -parent $PSCommandPath
$SourceDir = "$workingDirectory\source folder"
$targetDir = "$workingDirectory\target folder"
$logFile = "$workingDirectory\log.txt"
$options = "/copyall /b /is /r:5 /w:5 /log:$logFile"
& robocopy "$sourceDir" "$targetDir" /copyall /b /is /r:5 /w:5 "/log:$logFile"
好的,我在使用 Start-Process 时看到错误:
ERROR : Invalid Parameter #3 : "...\target"
然而,运行 robocopy 直接不需要特殊引用。请注意,/copyall 需要管理员权限。
robocopy $sourcedir $targetdir /copyall /b /is /r:5 /w:5 /log:$logFile