LoginSignup
0
0

More than 1 year has passed since last update.

PDFファイルのページ数を取得するVBScript

Posted at

ポイントは、

  • PDFファイルは、ADODB.Stream を使って UTF-8 のテキストモードで読み込む
  • 読み込んだデータから、正規表現でページヘッダを探してカウントする

この2つだけ。

※ VBSファイル自体は Shift-JIS で保存することを忘れずに

VBScript
'// PDFページ数取得.vbs
'// エクスプローラ上で、このスクリプトファイルに PDFファイル(複数可)をドラッグ&ドロップする

Set adoStream = CreateObject("ADODB.Stream")
Set fso = CreateObject("Scripting.FileSystemObject")
Set regEx = CreateObject("VBscript.RegExp")
regEx.Global = True
regEx.Pattern = "/Type\s*/Page[^s]"

reStr = "ファイル名" & vbTab & "ページ数"

For Each arg In WScript.Arguments
    If fso.GetExtensionName(arg) = "pdf" Then
        With adoStream
            .Charset = "UTF-8"
            .Type = 2    '//[1:バイナリモード] [2:テキストモード]
            .Open
            .LoadFromFile arg
            pageCnt = regEx.Execute(.ReadText).Count
            .Close
        End With
        reStr = reStr & vbCrLf & fso.GetFileName (arg) & vbTab & pageCnt
    End If
Next

Msgbox reStr
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0