我正在使用以下代码通过 Windows 窗体“浏览”功能选择一个文件夹,然后将该路径传递给 gci cmdlet
cls
Function Get-Directory($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenfolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$OpenfolderDialog.RootFolder = $initialDirectory
$OpenfolderDialog.ShowDialog()| Out-Null
$StartDir = $OpenfolderDialog.SelectedPath
Return $StartDir | Out-String
}
$myDir = Get-Directory -initialDirectory "Desktop"
$Child = gci -path $mydir -r -Filter *.jpg
Foreach ($item in $Child) {Move-Item -path $item.pspath -Destination $myDir -Force}
但我收到这些错误:
***在 C:\Test\Combine Pics2.ps1:17 char:13 + $Child = gci <<<< -path $mydir -r -Filter *.jpg + CategoryInfo : ObjectNotFound: (C:\Test :String ) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Move-Item:无法将参数绑定到参数“路径”,因为它为空。在 C:\Test\Combine Pics2.ps1:19 char:43 + Foreach ($item in $Child) {Move-Item -path <<<< $item.pspath -Destination $myDir -Force} + CategoryInfo : InvalidData: (:) [Move-Item],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.MoveItemCommand***
$myDir 变量是 String 类型的,为什么不传给 -path 参数。