Splatting with Move-Item 会报错
Splatting with Move-Item gives an error
好的,脚本本身超过 500 行,所以我不会发布整个内容。但是,我已将问题缩小到一行。
如果我使用下面的代码行,一切都会按预期进行。
Move-Item -Path $path -Include *.txt,*.doc,*.pdf -Destination $dest -Force
但是当我将它改为使用 splatting 时,它给我一个错误
$downloadDir = "G:\Downloads"
$dest = "G:\Test\"
$editList = Get-ChildItem -LiteralPath "$downloadDir" -include "[" -File | ForEach-Object -Process {Rename-item -LiteralPath $_.FullName -NewName ($_.Name -replace "[][]"," ") -ErrorAction SilentlyContinue}
$mainList = Get-ChildItem -Path "$downloadDir" -File | Select-Object Name
ForEach ($list in $mainList) {
$item = $list.Name
$path = "$downloadDir\*"
$opts = @{
Path = $path;
Include = '*.txt,*.doc,*.pdf';
Force = $true;
Destination = $dest
}
Move-Item @opts
}
Move-Item : Cannot move item because the item at 'G:\Downloads\test.txt' does not exist.
我觉得我可能遗漏了一些非常基本的东西,但我对哈希的了解还不够tables/splatting,无法发现错误。
有什么想法吗?
编辑:
澄清一下,G:\Downloads\test.txt 来自从 Get-ChildItem.
生成的 $path
我实际上是在直接交换两个版本的代码(Splatting/Non-splatting)。
编辑 2:
添加了与该 Move-Item
相关的所有脚本部分
编辑 3:
通过对 "Include" 行使用双引号来实现此功能:
Include = "*.txt","*.doc","*.pdf";
更改您的包含自:
Include = '*.txt,*.doc,*.pdf';
到
Include = "*.txt","*.doc","*.pdf";
Path 语句也可能很棘手。
例如
get-childitem c:\temp -include *.txt
将不起作用,而
get-childitem c:\temp\* -include *.txt
如果您在 c:\temp
中有任何 .txt 文件,则可以使用
这很愚蠢,但其中一些 cmdlet 就是这样。
好的,脚本本身超过 500 行,所以我不会发布整个内容。但是,我已将问题缩小到一行。
如果我使用下面的代码行,一切都会按预期进行。
Move-Item -Path $path -Include *.txt,*.doc,*.pdf -Destination $dest -Force
但是当我将它改为使用 splatting 时,它给我一个错误
$downloadDir = "G:\Downloads"
$dest = "G:\Test\"
$editList = Get-ChildItem -LiteralPath "$downloadDir" -include "[" -File | ForEach-Object -Process {Rename-item -LiteralPath $_.FullName -NewName ($_.Name -replace "[][]"," ") -ErrorAction SilentlyContinue}
$mainList = Get-ChildItem -Path "$downloadDir" -File | Select-Object Name
ForEach ($list in $mainList) {
$item = $list.Name
$path = "$downloadDir\*"
$opts = @{
Path = $path;
Include = '*.txt,*.doc,*.pdf';
Force = $true;
Destination = $dest
}
Move-Item @opts
}
Move-Item : Cannot move item because the item at 'G:\Downloads\test.txt' does not exist.
我觉得我可能遗漏了一些非常基本的东西,但我对哈希的了解还不够tables/splatting,无法发现错误。
有什么想法吗?
编辑: 澄清一下,G:\Downloads\test.txt 来自从 Get-ChildItem.
生成的 $path我实际上是在直接交换两个版本的代码(Splatting/Non-splatting)。
编辑 2: 添加了与该 Move-Item
相关的所有脚本部分编辑 3: 通过对 "Include" 行使用双引号来实现此功能:
Include = "*.txt","*.doc","*.pdf";
更改您的包含自:
Include = '*.txt,*.doc,*.pdf';
到
Include = "*.txt","*.doc","*.pdf";
Path 语句也可能很棘手。
例如
get-childitem c:\temp -include *.txt
将不起作用,而
get-childitem c:\temp\* -include *.txt
如果您在 c:\temp
中有任何 .txt 文件,则可以使用这很愚蠢,但其中一些 cmdlet 就是这样。