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:
- '026' and '026945.007' in the path comes from SQL. I construct these paths based on information in SQL and then look for files
- I only want pdf of the extension mentioned in the
$fileExtnesionvariable - 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
}
}`
Your
Test-Path $pdfFilePath(where$pdfFilePathequals, e.g.\\nml-fp\EngDesignLib\026\026945.007\*B26945*.pdf) answers the question:But it sounds like the question you really want to ask is:
To answer that you can replace your
If (Test-Path $pdfFilePath) { ... }with this: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:
where the name of the file is shown rather than the search pattern.