Powershell获取子项检查文件名不工作
我想做的是在一个文件夹中查找特定的文件,然后检查macthes创建的每个文件的名称,并将其放到另一个文件夹中。以下是我尝试做的一个示例:Powershell获取子项检查文件名不工作,powershell,get-childitem,Powershell,Get Childitem,我想做的是在一个文件夹中查找特定的文件,然后检查macthes创建的每个文件的名称,并将其放到另一个文件夹中。以下是我尝试做的一个示例: $FileABackupLoc = "C:\FolderA" $FileBBackupLoc = "C:\FolderB" Get-ChildItem -Path $SourceFolder -Include *Membership*.pgp -Name -Recurse | %{ Write-Host "Current file: $_"
$FileABackupLoc = "C:\FolderA"
$FileBBackupLoc = "C:\FolderB"
Get-ChildItem -Path $SourceFolder -Include *Membership*.pgp -Name -Recurse |
%{
Write-Host "Current file: $_"
if($_ -like '*FileA*')
{
[System.IO.FileInfo]$FileADestination = (Join-Path -Path $FileABackupLoc -ChildPath $_.Name.replace(“_”,“\”))
move-item -Path $_.FullName -Destination $FileADestination.FullName -Verbose
}
if($_ -like '*FileB*')
{
[System.IO.FileInfo]$FileBDestination = (Join-Path -Path $FileBBackupLoc -ChildPath $_.Name.replace(“_”,“\”))
move-item -Path $_.FullName -Destination $FileBDestination.FullName -Verbose
}
}
当我运行代码时,我得到了这个错误,但不知道为什么:
不能对空值表达式调用方法 试试这个:
Get-ChildItem -Path $SourceFolder -Include *Membership*.pgp -Recurse | % {
if ($_ -like '.*FileA.*')
{
$FileADestination = Join-Path -Path $FileBBackupLoc -ChildPath (Split-Path -Leaf -Path $_.FullName)
Move-Item -Path $_.FullName -Destination $FileADestination
}
...
我相信您在原始Get ChildItem中使用的-Name参数可能会妨碍您,从以下文档:
-Name
仅获取位置中项目的名称。如果将此命令的输出通过管道传输到另一个命令,则只发送项目名称
是的,那真让我受不了!