如何將所有PDF
的總頁面的值存入文件夾?我也想輸出寫入.txt
文件。用於統計PDF文件頁面的代碼改進
因此,我需要知道如何獲得所有PDF
的頁面數量,以及如何將所有頁面編號和PDF
名稱放入列中。
我想這樣的輸出:
PDF file name Number of page
-------------------------------------
firstpdffile 30 pages
secondpdffile 25 pages
thirdpdffile 10 pages
fourthpdffile 5 pages
-------------------------------------
Total 70 pages
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
'--------------------------------
我用這個.bat
文件來運行該腳本
@echo off
color 0A
@set currentdir="%cd%"
title PDF page counter
del temp1.txt
del temp2.txt
del temp3.txt
del output.txt
cscript pdfpagecount.vbs > temp1.txt
BatchSubstitute.bat "%cd%" "" temp1.txt > temp2.txt & BatchSubstitute.bat "\" "" temp2.txt > temp3.txt & Type temp3.txt | findstr /I /V /C:"Microsoft" >>output.txt & del temp1.txt & del temp2.txt & del temp3.txt
exit
@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
:$changed 20100115
:$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
@CheranShunmugavel我已經添加了一些細節,你需要別人嗎? – Aurelius
對於cscript幾乎一無所知,我懷疑你的腳本可以處理壓縮對象流(即PDF 1.5 ++)。使用PDF庫解析您的PDF:它會很容易地告訴您頁面的數量。 –