Exclude the file name created in the script in powershell?

68 views Asked by At

For the files with extensions (".ipt", ".iam", ".ipn", ".idw") there may be a pdf equivalent, where pdf file may have exact name or it may have characters added to it.

I want to exclude the files with extensions mentioned above and their pdf equivalent but if there are any other pdf files I want those. I also want rest of the files from the folder.

In the script I construct the pdf file which works. But I can't seem to filter it out. Like Powershell refuses to acknowledge that this file exist. It excludes the file extension I dont want but wont exclude the pdf that was constructed. The path looks like \\nml-fp\EngDesignLib\068\068179.001 where 068, and 068179.001 come from SQL db. Therefore, have to stick with the path that is being created.

In the script below I have tried most of the cmdlets used to filter out. -and -not ($_.Name -contains $pdf.Name) ($_.Name -notcontains $pdf.Name) ($_.Name -notLike $pdf.Name) -not ($_.Name -like $pdf.Name) ($_.Name -notIn $pdf.Name) ($_.Name -notIn $arrayIdwPdf )

If($testPathEngDesign){
 Get-ChildItem -Path $PathEngDesign -File -Recurse -ErrorAction SilentlyContinue -Force 
 |ForEach-Object{
 $file = $_
 $extension = $_.Extension
 $baseName = $file.Basename
 $filename = $file.Name
 if( $extension -in '.ipt', '.iam', '.ipn', '.idw') {
 $filePdf = "*$($baseName)*.pdf"
 $pdfFilePath = $file.FullName -replace ($file.Name, $filePdf)
 $pdf = Get-ChildItem -Path $pdfFilePath
 $fileNamePdf = $pdf.Name
 $filePdfPath = $pdf.FullName
 $arrayIdwPdf += $filenamePdf
 $excludeList = @("*.ipt", "*.iam", "*.ipn", "*.idw",$pdf.Name)
  Get-ChildItem -Path $PathEngDesign -Exclude $excludeList -File -Recurse  | ForEach-Object 
  {if ($_.Name -notlike $pdf.Name ) {
  $csvData = [PSCustomObject]@{
  PartRn          = [String]$partRn
  DocumentNumber = [String]$PartNo
  Revision        = [String]$revision
  Classification  = [String]$classification
  FileName        = [String]$_.Name
  Path            = [String]$_.FullName
  }
  $csvDataArray += $csvData
  $csvData | Export-Csv -Path $csvFilePath -Force -Append 
1

There are 1 answers

0
Keith Miller On

From your narrative, it sounds to me that you want something like this:

$include = 'ipt|iam|ipn|idw'    # Regex match string

$splat = @{
    Path            = $PathEngDesign
    File            = $true
    Recurse         = $true
    ErrorAction     = 'SilentlyContinue'
    Force           = $true
}
# No need for repeated calls to Get-ChildItem

$AllFIles           = [system.Collections.ArrayList]::new(( Get-ChildItem @splat | where Extension -match "pdf|$include"))
$pdfFIles           = $AllFiles | where Extesion -match 'pdf'
$BaseNameGroups     = $AllFiles | where Extesion -match $include | Group BaseName

# When basenames match, the files are removed from the AllFiles collection
$BaseNameGroups | ForEach {    
    $pdfMatch   = $pdfFiles | where BaseName -match [Regex]::Escape($_.Name)
    If ($pdfMatch) {
        $AllFiles.Remove($pdfMatch)
        $_.Group.ForEach{$AllFiles.Remove($_)}
    }
}
# AllFiles now contains only .pdf files with no matching files and miscellaneous types with no matching .pdf

$AllFiles | ForEach {[PSCustomObject]@{

    ***  build object ***

}} | Exprt-Csv ...