Powershell移动带有特殊字符的文件

时间:2014-11-23 10:37:54

标签: powershell

如何移动包含特殊字符的文件?我不允许重命名该文件。

我的文件是:File.Server.Windows.2003。[SP2] .01232005.txt

# dFold = The destination folder in the format of \\drive\folder\SubFolder\   
# tDir = Root Target Directory on NAS
# sFold = The name of the subfolder that the files will go into
$dFold = "$tDir$sFold"

# sDir = Source Directory
# $File = Original file name as seen in the source Directory
Move-Item -Path $sDir$File -Destination $dFold -force

当我尝试执行上面的代码时,它不会移动文件。我可以添加一些Write-Host语句,并说它会移动文件,但它确实没有。

Write-Host "Now moving " $File "to " $dFold"\"
  Move-Item -Path $sDir$File -Destination $dFold -force

  # Now we just write put what went where or not          
  Write-Host $File "Was Moved to:" $dFold

输出:

Now moving  File.Server.Windows.2003.[SP2].01232005.txt to  \\NAS\Inventory\Servers\  
File.Server.Windows.2003.[SP2].01232005.txt Was Moved to: \\NAS\Inventory\Servers  

1 个答案:

答案 0 :(得分:4)

你有没有尝试过:

Move-Item -LiteralPath $sDir$File -Destination $dFold
使用-Path参数时,

Move-Item允许wildcard matches,因此子字符串[SP2]被解释为单个字符'S''P',或'2'而不是字符串'[SP2]'。使用参数-LiteralPath代替-Path可以防止这种情况。