Skip to main content
Home
Blog

Main navigation

  • Home
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Forums
  3. Knowledge Base
  4. How-To

PDF pages counting using VB script.

By ongetc , 26 February, 2017

There are many utilities that you can download or buy to count your PDF files if you have the need to do so or open file by file to look for the page count but why! You can do it yourself with a simple VB script.

Here is a complete working VB script where you can just copy and create a file call "pdfpagecount.vbs" that you can use where you just drop this file in any folder that you have PDF files and just run cscript pdfpagecount.vbs and it will produce a "pagecount.txt" to contain all PDF files and page count.


' By Chanh Ong
'File: pdfpagecount.vbs
' Purpose: count pages in pdf file in folder
Const OPEN_FILE_FOR_READING = 1

Set gFso = WScript.CreateObject("Scripting.FileSystemObject")
Set gShell = WScript.CreateObject ("WSCript.shell")
Set gNetwork = Wscript.CreateObject("WScript.Network")

directory="."
set base=gFso.getFolder(directory)
call listPDFFile(base)

Function ReadAllTextFile(filespec)
Const ForReading = 1, ForWriting = 2
Dim f
Set f = gFso.OpenTextFile(filespec, ForReading)
ReadAllTextFile = f.ReadAll
End Function

function countPage(sString)
Dim regEx, Match, Matches, counter, sPattern
sPattern = "/Type\s*/Page[^s]" ' capture PDF page count
counter = 0

Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = sPattern ' Set pattern "^rem".
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
set Matches = regEx.Execute(sString) ' Execute search.
For Each Match in Matches ' Iterate Matches collection.
counter = counter + 1
Next
if counter = 0 then
counter = 1
end if
countPage = counter
End Function

sub listPDFFile(grp)
Set pf = gFso.CreateTextFile("pagecount.txt", True)
for each file in grp.files
if (".pdf" = lcase(right(file,4))) then
larray = ReadAllTextFile(file)
pages = countPage(larray)
wscript.echo "The " & file.name & " PDF file has " & pages & " pages"
pf.WriteLine(file.name&","&pages)
end if
next
pf.Close
end sub

Forums
How-To

Reseller hosting (free)

Hosting24 (w/ ssh)

A2 Hosting

Monthly archive

  • February 2019 (1)
  • December 2020 (224)
  • January 2021 (168)
  • March 2021 (1)

Pagination

  • Previous page
  • 2

Popular content

Last viewed:

  • X Library
  • Seagull
  • OpenCms
  • SuperEdi
  • phpGrabComics
  • Yet Another Forum.net
  • LitePublisher
  • osCmax
  • MosModule: How to use snippet?
  • Hostinger

User login

  • Reset your password