Move-Item with -match和-LiteralPath不起作用

时间:2017-03-13 11:54:38

标签: powershell move

我有一个文件移动脚本,用于查找名称中包含关键字的文件并将其移动到特定文件夹。大约有一百行都可以正常工作,除了其中一行,我唯一能想到的就是文件中有一个括号。

我已经使用-LiteralPath来获取完整路径,但错误很奇怪。我尝试过双引号,双反引号以及我在其他地方找到的其他一些选项,但没有任何效果。

这是脚本的所有相关部分(不发布整个内容):

# Source folders
$RecipesFolder = "F:\Downloads\Downloaded Recipes\"

# Destination Folders Drive I
$CoffeeRecipes = "I:\My Recipes\Coffee Recipes\"

# Get list of files
$Files = Get-ChildItem $RecipesFolder -File -Recurse

# Process Files
foreach ($File in $Files) {
    if ($File -match "Coffee") {
        Move-Item -LiteralPath $RecipesFolder$File -Destination "$CoffeeRecipes" -Force
    }

这是一个非常简单的脚本,除了那个之外,其他所有行都可以正常工作。所有的行都是相同的。

这是我收到的完整错误(sourcepath混淆了安全性),并提前感谢您的任何帮助:

Move-Item : Cannot move item because the item at 'F:\Downloads\Downloaded Recipes\[1] 
Coffee Recipe - 11[1].txt' does not exist.
At Path-to-powershell-script-file:153 char:13
+             Move-Item -LiteralPath $RecipesFolder$File -Destina ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand

当我收到此错误时,文件消失,并且没有移动到文件夹,它就会消失。

0 个答案:

没有答案