Home > OS >  Powershell - Search for files from list - Trouble displaying just the match
Powershell - Search for files from list - Trouble displaying just the match

Time:12-15

I have a script that I use to search for hundreds of files across multiple drives from a list. It works fine as it catches all the matches. The only issue is I need to see the file it matched along with the extension. A bit of the back story... We have programs that share the same name as a Copybook. Not too uncommon in the mainframe world. When searching for a file, I have to Wildcard the search in order to catch all the same name files (Minus the extension). I then have to manually search for the hits to determine if they are copybooks or programs. When I try to add any logic to the script below, it displays the entire array of file names and not just the actual match. Would anyone be able to assist in capturing and displaying just the matched file along with it's extension? Maybe it's location also?

Regards, -Ron

#List containing file names must be wilcarded FILE.*

#Parent folder (Where to begin search)
    $folder = 'C:\Workspace\src'
#Missing Artifacts Folder (Where Text file resides)
    $Dir2 = 'C:\Workspace\Temp'
#Text File Name
    $files=Get-Content $Dir2\FilesToSearchFor.txt

    cd \
    cd $folder

    Write-Host "Folder: $folder"
    # Get only files and only their names
     $folderFiles = (Get-ChildItem -Recurse $folder -File).Name
    foreach ($f in $files) {
       #if ($folderFiles -contains $f) { 
       if ($folderFiles -like $f) { 
            Write-Host "File $f was found." -foregroundcolor green
        } else { 
            Write-Host "File $f was not found!" -foregroundcolor red 
        }
    }

CodePudding user response:

Instead of testing whether the entire list of file names contains the target file name ($folderFiles -like $f), load all files into a hashtable and then test if the target file name exists as a key with ContainsKey():

$fileTable = @{}
Get-ChildItem -Recurse $folder -File |ForEach-Object {
  # Create a new key-value entry for the given file name (minus the extension) if it doesn't already exist
  if(-not $fileTable.ContainsKey($_.BaseName)){
    $fileTable[$_.BaseName] = @()
  }
  # Add file info object to the hashtable entry
  $fileTable[$_.BaseName]  = $_
}

foreach($f in $files){
  if($fileTable.ContainsKey($f)){
    Write-Host "$($fileTable[$f].Count) file(s) matching '$f' were found." -ForegroundColor Green
    foreach($file in $fileTable[$f]){
      Write-Host "File with extension '$($file.Extension)' found at: '$($file.FullName)'"
    }
  }
  else {
    Write-Host "No files found matching '$f'"
  }
}

Since the $fileTable contains not just the name, but also a reference to the original file info objects with that names as returned by Get-ChildItem, you can easily access relevant metadata (like the Extension property) now

  • Related