Count PDF file in zip file

Warzou 101 Reputation points
2022-01-05T08:54:48.23+00:00

Hi,

I'm looking for a way to get the number of pdf file in zip file

I found this powershell on google :

$ZipRoot = 'C:\ZipFolder\'
$ZipFiles = Get-ChildItem -Path $ZipRoot -Filter *.zip -Recurse

Add-Type -AssemblyName System.IO.Compression.FileSystem
$Results = foreach( $ZipFile in $ZipFiles ){
$PDFCount = ([IO.Compression.ZipFile]::OpenRead($Zipfile.FullName).Entries|where- 
object{$_.Name -like "*.pdf"}).Count

     [pscustomobject]@{
     FullName = $ZipFile.fullname
     PDFCount = $PDFCount



    }

}

 $Results

The result is the count of pdf per zip file.

FnzkvrL

But my goal is the count of pdf file per folder. for example :

In "Folder 1", there is 4 zip and in each zip, 4 pdf. The result will be 16 pdf for "Folder 1".

Thank you

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-01-05T19:46:26.283+00:00

    Maybe not the most elegant way, but it should give you what you asked for:

    $ZipRoot = 'C:\ZipFolder\*'
    
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    Get-ChildItem -Path $ZipRoot -Filter *.zip -Recurse -File |
        ForEach-Object{
            $PDFCount = ([IO.Compression.ZipFile]::OpenRead($_.FullName).Entries|
                Where-Object{$_.Name -like "*.pdf"}).Count
            [pscustomobject]@{                          # no need for the file name if you only want the number of files by directory
                DirectoryName = $_.directoryname
                PDFCount = $PDFCount
            }
        } |
            Group-Object -Property DirectoryName|
                ForEach-Object{
                    $Tot = 0
                    $DirectoryName = $_.Name
                    $_.Group |
                        ForEach-Object{
                            $Tot += $_.PDFCount
                        }
                    [PSCustomObject]@{                  # ignore the original, this one's a summary
                        DirectoryName = $DirectoryName
                        PDFCount = $Tot
                    }
                }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Warzou 101 Reputation points
    2022-01-06T15:37:10.13+00:00

    Thank you very much @Rich Matheisen , this is what I was looking for.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.