-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDFPCP.ps1
76 lines (64 loc) · 2.6 KB
/
PDFPCP.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#Dialog box for folder selection
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
RootFolder = "Desktop"
Description = "PDF Page Counter Powershell - Pick a folder"
}
if($FolderBrowser.ShowDialog() -eq "OK")
{
$folder = $FolderBrowser.SelectedPath
}
else { break }
#Get name from selected folder for name of report
$name = Get-Item $FolderBrowser.SelectedPath
#Get date for name of report
$date = Get-Date -Format 'dd-MM-yyyy'
#Question box YesNo for subfolders
$answer = [System.Windows.Forms.MessageBox]::Show("Subfolders?", "PDFPCP", "YesNo", "Question")
if($answer -eq "Yes")
{
#Script for counting pages and making a report that goes in the chosen folder if subfolders is selected
$outputFile = Join-Path -Path $folder -ChildPath "$($name.BaseName)_report_$($date).txt"
$Total = $Files = 0
foreach($File in (Get-ChildItem -Path $folder -Recurse -Filter *.pdf)){
$Pages = (.\pdfinfo $File.FullName | Select-String -Pattern '(?<=Pages:\s*)\d+').Matches.Value
$Total += $Pages
$Files++
$object = [PSCustomObject]@{
PdfFile = $File.Name
Pages = $Pages
}
#Write process to terminal
Write-Host $object
#Make CSV file
$csvData = $object | ConvertTo-Csv -Delimiter ';' -NoTypeInformation
$csvData | Select-Object -Skip 1 | Out-File -FilePath $outputFile -Append
}
"`nTotal Number of pages: {0} in {1} files" -f $Total,$Files | Out-File -FilePath $outputFile -Append
}
if($answer -eq "No")
{
#Script for counting pages and making a report that goes in the chosen folder if subfolders is not selected
$outputFile = Join-Path -Path $folder -ChildPath "$($name.BaseName)_report_$($date).txt"
$Total = $Files = 0
foreach($File in (Get-ChildItem -Path $folder -Filter *.pdf)){
$Pages = (.\pdfinfo $File.FullName | Select-String -Pattern '(?<=Pages:\s*)\d+').Matches.Value
$Total += $Pages
$Files++
$object = [PSCustomObject]@{
PdfFile = $File.Name
Pages = $Pages
}
#Write process to terminal
Write-Host $object
#Make CSV file
$csvData = $object | ConvertTo-Csv -Delimiter ';' -NoTypeInformation
$csvData | Select-Object -Skip 1 | Out-File -FilePath $outputFile -Append
}
"`nTotal Number of pages: {0} in {1} files" -f $Total,$Files | Out-File -FilePath $outputFile -Append
}
#Finished pop up
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Finished!",0,"PDFPCP",0x1)
#Opens selected folder in Explorer window
Invoke-Item $name