Skip to content

Instantly share code, notes, and snippets.

@cwg999
Last active May 1, 2019 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwg999/d6cabce5004623be7ffc00f5840ebb17 to your computer and use it in GitHub Desktop.
Save cwg999/d6cabce5004623be7ffc00f5840ebb17 to your computer and use it in GitHub Desktop.
Get Number of PDF Pages (Greater than 1) Parallel with Throttle Limit
$ArrayList = [System.Collections.ArrayList]@()
# Note: this scripts searches for folders named "Single" recursively from the starting folder.
Get-ChildItem -Path .\ -Recurse | Where-Object {
$_ -is [System.IO.FileInfo] `
-and ($_.Directory.Name -eq 'Single')
} | ForEach-Object {
$ArrayList.add($_.FullName)
}
# Threaded task to use pdfk to get files.
workflow Test-Workflow{
param(
[Parameter (Mandatory = $true)]
[System.Collections.ArrayList]$ArrayList
)
# Multithreaded - that was easy.
ForEach -Parallel -throttlelimit 5 ($file in $ArrayList)
{
$command = '"E:\Program Files (x86)\PDFtk Server\bin\pdftk.exe" "'+$file+'" dump_data | findstr NumberOfPages'
$output = iex "& $command"
$pages = $output.Substring(15,$output.length-15)
# Only output files with more than one page. (These files were supposed to be "single" page pdfs)
if($pages -gt 1){
Write-Output $file $pages
}
# Write-Output $output
}
}
Test-Workflow -ArrayList ($ArrayList)
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment