PowerShell:批量复制名称为%sign的文件

时间:2016-12-05 00:57:01

标签: powershell

我有一个包含数千个文件名的文本文件。我正在尝试使用这个小脚本将这些特定文件复制到子目录中:

$names = "moves.txt"
$ext = ".zip"
$dest = "select"

foreach ($name in Get-Content $names)
  {
  echo "$name$ext"
  Move-Item -Path "$name$ext" -Destination "$dest"
  }

不幸的是,许多文件名都包含特殊字符,包括%符号。这导致PS进入barf。对于列表中的任何名称,有没有办法可以逃脱它?

感谢。

1 个答案:

答案 0 :(得分:2)

Powershell: moving items not working when filenames that have chars [ ]

似乎表明move-item的-literalpath参数可能就是你所寻求的。

如果这是答案,则信用不是我的,而是CB&#39。

/ edit因此,对以下内容的简单更改应该有效:

$names = "moves.txt"
$ext = ".zip"
$dest = "select"

foreach ($name in Get-Content $names)
  {
  echo "$name$ext"
  Move-Item -LiteralPath "$name$ext" -Destination "$dest"
  }

有关详细信息,请参阅: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/move-item

相关问题