powershellget-childitem

How do I find a file when only part of the file name is known using Powershell?


For certain types of files there maybe a pdf equivalent. However this pdf file may have characters added to the left, right or may be exactly the same. For example for file TAX457.idw, there maybe TAX457-1.pdf or just TAX457.pdf

I also need the file name of this pdf file. $filePdf variable just doesn't work. I have tried ".*$([regex]::Escape($baseName)).*\.pdf" this just doesn't work. The output is as if there is no file in the folder. "*$($baseName)*.pdf" this returns files that are not pdf for but adds .pdf and * to the file name. For example there are three files in folder B26945.idw,B26945.ipt, and B2694501.pdf the output is \\EngDesignLib\026\026945.007\*B26945*.pdf \\nml-fp\EngDesignLib\026\026945.007\*B26945*.pdf

The intended output is \\EngDesignLib\026\026945.007\B26945.idw \\EngDesignLib\026\026945.007\B26945.ipt \\EngDesignLib\026\026945.007\B2694501.pdf

EDIT: Some more information:

  1. '026' and '026945.007' in the path comes from SQL. I construct these paths based on information in SQL and then look for files
  2. I only want pdf of the extension mentioned in the $fileExtnesion variable
  3. Not all of the files will have a pdf equivalent.
$fileExtensions = @("*.ipt", "*.iam", "*.ipn", "*.idw")
Get-ChildItem -Path $PathEngDesign -File -Include $fileExtensions -Recurse -ErrorAction SilentlyContinue -Force|ForEach-Object{
$file = $_
$baseName = $file.Basename
                $filename = $file.Name
                $filePdf = "*$($baseName)*.pdf"
 $pdfFilePath = $file.fullname -Replace ($filename, $filePdf)
Write-host "Path" $file.FullName
  If (Test-Path $pdfFilePath){
                    Write-host "PDF File and Path Found" $filePdf "PATH-->" $pdfFilePath
}
}`

Solution

  • Your Test-Path $pdfFilePath (where $pdfFilePath equals, e.g. \\nml-fp\EngDesignLib\026\026945.007\*B26945*.pdf) answers the question:

    does a file exist that matches this pattern?

    But it sounds like the question you really want to ask is:

    what is the name of the file that matches this pattern?

    To answer that you can replace your If (Test-Path $pdfFilePath) { ... } with this:

        $pdf = Get-ChildItem -Path $pdfFilePath
        if( $null -ne $pdf )
        {
            Write-host "PDF File and Path Found" $filePdf "PATH-->" $pdf.FullName
        }
    

    Note - there might be more than one match, but the code above assumes there's only one at most. If you have multiple matches you'll need to handle that differently...

    If I run that on my machine I get this output instead:

    Path C:\src\so\test\B26945.idw
    PDF File and Path Found *B26945*.pdf PATH--> C:\src\so\test\B2694501.pdf
    

    where the name of the file is shown rather than the search pattern.