Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6532959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:04:02+00:00 2026-05-25T10:04:02+00:00

I was just wondering why the vbs code in the link below is not

  • 0

I was just wondering why the vbs code in the link below is not counting pdf pages correctly? It seems to under count by half or more the number of pages that actually exist in each pdf.

http://docs.ongetc.com/index.php?q=content/pdf-pages-counting-using-vb-script

Here is the code if you can not access the link above:

' 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

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:04:03+00:00Added an answer on May 25, 2026 at 10:04 am

    Try this

    Function getPdfPgCnt(ByVal sPath)
        Dim strTStr
    
        With CreateObject("Adodb.Stream")
            .Open
            .Charset = "x-ansi"
            .LoadFromFile sPath
            strTStr = .ReadText(-1)
        End With
    
        With (New RegExp)
            .Pattern = "Type\s+/Page[^s]"
            .IgnoreCase = True
            .Global = True
            getPdfPgCnt = .Execute(strTStr).Count
        End With
    
        If getPdfPgCnt = 0 Then getPdfPgCnt = 1
    End Function
    
    'Usage : getPdfPgCnt("C:\1.pdf")
    

    Update #1~#2:

    Option Explicit
    
    Private Function getPdfPgCnt(ByVal sPath) 'Returns page count of file on passed path
        Dim strTStr
    
        With CreateObject("Adodb.Stream")
            .Open
            .Charset = "x-ansi"
            .LoadFromFile sPath
            strTStr = .ReadText(-1)
        End With
    
        With (New RegExp)
            .Pattern = "Type\s*/Page[^s]"
            .IgnoreCase = True
            .Global = True
            getPdfPgCnt = .Execute(strTStr).Count
        End With
    
        If getPdfPgCnt = 0 Then getPdfPgCnt = 1
    End Function
    
    '--------------------------------
    Dim oFso, iFile
    Set oFso = CreateObject("Scripting.FileSystemObject")
    
    'enumerating pdf files in vbs's base directory
    For Each iFile In oFso.getFolder(oFso.GetParentFolderName(WScript.ScriptFullName)).Files
        If LCase(oFso.GetExtensionName(iFile)) = "pdf" Then WScript.Echo iFile & " has "& getPdfPgCnt(iFile)&" pages."
    Next
    Set oFso = Nothing
    '--------------------------------
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if a .NET app can be compiled down to native machine code
Just wondering why people like case sensitivity in a programming language? I'm not trying
Just wondering if any of you people use Count(1) over Count(*) and if there
just wondering which approach would be better if both blocks of code would yield
Just wondering how I can replace the AutoResetEvent in the below? I was trying
Just wondering whether it is possible to update a ByteArray in C Code, which
Just wondering sometime the 2NF may not be necessary. Option 1. ORDERS {orderId (pk),
Just wondering if someone knows what is wrong with my javascript math script below:
just wondering if I have a webpage that generates a pdf, but could take
Just wondering I have searched but I have not found an answer. My question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.