Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

i want to know numbers of pages for pdfs with cmd in windows with ghostscript i want to…

  1. User

    i want to know numbers of pages for pdfs with cmd in windows with ghostscript i want to know all the pdf in the folder where i run the code

  2. Assistant

    You can use a Windows batch script in combination with Ghostscript to count the number of pages in each PDF file within a folder. Below are detailed steps to achieve this:


    Prerequisites

    1. Install Ghostscript:

      • Download the latest version of Ghostscript from Ghostscript Downloads.
      • During installation, ensure that Ghostscript is added to your system's PATH. This allows you to run gswin64c (or gswin32c for 32-bit systems) from any command prompt without specifying the full path.
    2. Verify Installation:

      • Open Command Prompt (cmd).
      • Type gswin64c -version and press Enter.
      • You should see the version information of Ghostscript. If not, ensure that Ghostscript is correctly installed and added to your PATH.

    Create the Batch Script

    1. Open a Text Editor:

      • Use Notepad or any other text editor of your choice.
    2. Paste the Following Script:

      @echo off
      setlocal enabledelayedexpansion
      
      REM Check if Ghostscript is installed
      where gswin64c >nul 2>&1
      if errorlevel 1 (
          echo Ghostscript (gswin64c) is not found in PATH.
          echo Please install Ghostscript and ensure it's added to the system PATH.
          pause
          exit /b
      )
      
      REM Iterate over all PDF files in the current directory
      for %%F in (*.pdf) do (
          REM Use Ghostscript to get the page count
          for /f "delims=" %%A in ('gswin64c -q -dNODISPLAY -c "(\\"%%F\\") (r) file runpdfbegin pdfpagecount = quit"') do (
              echo %%F: %%A pages
          )
      )
      
      endlocal
      pause
      
    3. Save the Script:

      • Save the file with a .bat extension, for example, CountPdfPages.bat.
      • Important: Save this script in the directory where your PDF files are located.

    Run the Batch Script

    1. Navigate to the Folder:

      • Open File Explorer and navigate to the folder containing your PDFs and the CountPdfPages.bat script.
    2. Execute the Script:

      • Double-click on CountPdfPages.bat.
      • A Command Prompt window will open and display the number of pages for each PDF file in the folder.
      • Example Output:
        document1.pdf: 12 pages
        report.pdf: 45 pages
        brochure.pdf: 8 pages
        Press any key to continue . . .
        

    Understanding the Script

    • @echo off: Hides the command executions in the Command Prompt for cleaner output.

    • setlocal enabledelayedexpansion: Enables the use of delayed environment variable expansion, which is useful in loops.

    • Check Ghostscript Installation:

      • The script first checks if gswin64c (Ghostscript's console version) is available in the system's PATH.
      • If not found, it prompts the user to install Ghostscript and exits.
    • Loop Through PDFs:

      • for %%F in (*.pdf) do (...): Iterates over each PDF file in the current directory.
    • Get Page Count:

      • The inner for /f loop runs Ghostscript with specific parameters to retrieve the page count:
        • -q: Quiet mode, minimizes Ghostscript's output.
        • -dNODISPLAY: Does not open a display window.
        • -c "(\\"%%F\\") (r) file runpdfbegin pdfpagecount = quit": Ghostscript commands to:
          • Open the PDF file.
          • Initialize PDF processing.
          • Retrieve the pdfpagecount (number of pages).
          • Output the page count and quit.
    • Display Results:

      • echo %%F: %%A pages: Prints the filename and its corresponding page count.
    • pause: Keeps the Command Prompt window open until a key is pressed, allowing you to read the results.


    Alternative: Using PowerShell

    If you prefer using PowerShell instead of a batch script, here's an alternative approach:

    1. Open PowerShell:

      • Press Win + X and select Windows PowerShell or Windows PowerShell (Admin).
    2. Navigate to the Folder:

      cd "C:\Path\To\Your\PDFs"
      
    3. Run the Following Command:

      Get-ChildItem -Path . -Filter *.pdf | ForEach-Object {
          $pdf = $_.FullName
          # Replace double quotes with escaped quotes
          $escapedPdf = $pdf -replace '"','\"'
          $pageCount = & gswin64c -q -dNODISPLAY -c "(`"$escapedPdf`) (r) file runpdfbegin pdfpagecount = quit"
          Write-Output "$($_.Name): $pageCount pages"
      }
      

      Explanation:

      • Get-ChildItem: Retrieves all PDF files in the current directory.
      • ForEach-Object: Iterates over each PDF file.
      • Ghostscript Command: Similar to the batch script, it retrieves the page count.
      • Write-Output: Displays the filename and its page count.

      Example Output:

      document1.pdf: 12 pages
      report.pdf: 45 pages
      brochure.pdf: 8 pages
      

    Notes & Tips

    • 64-bit vs. 32-bit Ghostscript:

      • If you're using a 32-bit version of Ghostscript, replace gswin64c with gswin32c in the scripts.
    • Handling Special Characters:

      • The batch script escapes quotes (\") to handle filenames with spaces or special characters.
    • Performance:

      • For folders with a large number of PDFs, the script might take some time to process all files.
    • Error Handling:

      • The script does basic error checking for Ghostscript's presence but does not handle corrupted PDF files. You may enhance the script to include additional error handling as needed.

    By following the above steps, you can efficiently list all PDF files in a folder along with their respective page counts using the Windows Command Prompt and Ghostscript.