Reputation: 93
What I want to do is look for particular files in a folder, then check the name of each file that macthes and drop it in another folder. Here is a sample of what I'm trying to do:
$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
}
}
When I run the code, I get this error but have no idea why: You cannot call a method on a null-valued expression.
Upvotes: 1
Views: 1213
Reputation: 4045
Maybe try this:
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
}
...
I believe your use of the -Name argument in the original Get-ChildItem may be getting in your way here, from the docs:
-Name
Gets only the names of the items in the locations. If you pipe the output of this command to another command, only the item names are sent.
Upvotes: 1