Get-ChildItem and Searching from an Array

Hey guys, new here.

I’ve been crawling all over the web for this one and I can’t seem to find an answer.

Here’s the scenario, I’ve got 1 folder with a mishmash of client names and a CSV with the names of those clients.

The catch is when I do an exact name to name search, I only get 23 results of the 1577 names in the CSV.

So here’s my latest stab at it:

Since not all the names are correct 100%, I figured let’s go with the first 7 characters. This gives me 0 results.

$a = Import-CSV C:\scripts\HelenClients.csv
$a = @($a.Client)
$a = $a | %{ $_.SubString(0,6) }

$c = Get-ChildItem E:\Legal\ -include ($a) -recurse # | Where-Object {($_ -match $a)}


ForEach($file in $c){
    $dest =  Split-Path -path $file.FullName -Parent | Split-path -NoQualifier
    #Copy-Item -path $file -recurse -Destination "e:\sorted\11\$dest" -force -Verbose
}
 
$a = Import-CSV C:\scripts\HelenClients.csv
$a = @($a.Client)
#$a = $a | %{ $_.SubString(0,6) }

$c = Get-ChildItem E:\Legal\ -include ($a) -recurse # | Where-Object {($_ -match $a)}


ForEach($file in $c){
    $dest =  Split-Path -path $file.FullName -Parent | Split-path -NoQualifier
    #Copy-Item -path $file -recurse -Destination "e:\sorted\11\$dest" -force -Verbose
}

Here’s the original, this nets me 27 results.

$a = Import-CSV C:\scripts\Clients.csv
$a = @($a.Client)
#$a = $a | %{ $_.SubString(0,6) }

$c = Get-ChildItem E:\Legal\ -include ($a) -recurse # | Where-Object {$_ -match $a}


ForEach($file in $c){
    $dest =  Split-Path -path $file.FullName -Parent | Split-path -NoQualifier
    #Copy-Item -path $file -recurse -Destination "e:\sorted\11\$dest" -force -Verbose
}

So the files are kept in an order like so:

Animals
      ---Dogs
         ---Lots of Folders About Different Breeds
            ---PDFs and Docs that match the folder name
      ---Cats
         ---Lots of Folders About Different Breeds
            ---PDFs and Docs that match the folder name
      ---Penguins
         ---Lots of Folders About Different Breeds
            ---PDFs and Docs that match the folder name      

Now the person who wrote these files was meticulous about following a naming convention.

An example would be
Dogs
Golden_Retriever
Golden_Retriever_Bones.pdf
Golden_Retriever_Hobbies.pdf

The issue now that I’m running into is that the client list has some issues matching up with what we’ve got. So a name to name search doesn’t work as stated before, so how about a partial search?

Should I loop it in a filter?

Please note that the target directory has 2996 items in it. I don’t want all of the items :frowning:

Need to understand more about what you are passing in the clients array. Is clients “Golden_Retriever” and you expect Golden_Retriever* to be returned? The -Include is buggy and almost never works like you expect. The -Match can work, but it is a regular expression and $_ represents the entire object, so it should be $_.Name to see if Golden_Retriever_Bones.pdf matches your criteria.

Try one of the following methods to see if it works as expected:

Return all files and then search the object

$clients = Import-CSV C:\scripts\HelenClients.csv | Select -ExpandProperty Client

#Get all files
$allFiles = Get-ChildItem -Path 'E:\Legal\*' -File -Recurse 

$results = foreach ($client in $clients) {
    $filter = '{0}*' -f $client
    #Return all files matching the client
    $allFiles | Where{$_.Name -like $filter}
}

Or try searching by building a wildcard for each directory

$clients = Import-CSV C:\scripts\HelenClients.csv | Select -ExpandProperty Client

$results = foreach ($client in $clients) {
    $filter = 'E:\Legal\*{0}*' -f $client
    #Return all files matching the client
    Get-ChildItem -Path $filter -File -Recurse 

}