0

PDFすべてのページの合計値をフォルダに入れるにはどうすればよいですか?また、出力は.txtファイルに書き込まれます。

したがって、すべてのページ数を取得する方法と、すべてのページ番号と名前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.
)
4

1 に答える 1

3

集中しようとしています

  1. コンソールスクリプトの出力のようなテーブル(ヘッダー、本文、フッター)
  2. 値の合計

何が必要

  1. .NET sprintfのようなフォーマット(クラスcFormat)
  2. ループの前に0に初期化された合計変数、ループに追加、ループの後に出力/使用

コード内:

' 14490628.vbs
' http://stackoverflow.com/questions/14490628/code-improvements-for-counting-the-pages-of-pdf-files

Option Explicit

Dim oFS     : Set oFS  = CreateObject("Scripting.FileSystemObject")
Dim sDir    : sDir     = "..\..\10041057\data"
Dim oFmt    : Set oFmt = New cFormat
Dim nFWidth : nFWidth  = 25                ' for file name
Dim nPWidth : nPWidth  = 15                ' for number of pages
Dim nLWidth : nLWidth  = nFWidth + nPWidth ' for whole line

Dim sRuler1 : sRuler1 = String(nLWidth, "=")
Dim sRuler2 : sRuler2 = String(nLWidth, "-")
Dim sFmtT   : sFmtT   = insertWidth("{4}{2}{0,-@F}{1,@P}{2}{3}", nFWidth, nPWidth)
Dim aDataT  : aDataT  = Array("PDF file name", "Number of pages", vbCrLf, sRuler2, sRuler1)
Dim sFmtF   : sFmtF   = insertWidth("{0,-@F}{1,@P}", nFWidth, nPWidth)
Dim aDataF  : aDataF  = Array("", 0)
Dim sFmtS   : sFmtS   = insertWidth("{3}{2}{0,-@F}{1,@P}{2}{4}", nFWidth, nPWidth)
Dim aDataS  : aDataS  = Array("Total", 0, vbCrLf, sRuler2, sRuler1)

WScript.Echo oFmt.formatArray(sFmtT, aDataT)
Dim oFile
For Each oFile In oFS.GetFolder(sDir).Files
    aDataF(0) = oFile.Name
    aDataF(1) = getPdfPgCnt(oFile.Path)
    WScript.Echo oFmt.formatArray(sFmtF, aDataF)
    aDataS(1) = aDataS(1) + aDataF(1)
Next
WScript.Echo oFmt.formatArray(sFmtS, aDataS)

Function getPdfPgCnt(sFSpec)
  ' stolen from the VBScript Docs
  Dim lowerbound : lowerbound = 1
  Dim upperbound : upperbound = 1000
  getPdfPgCnt = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Function

Function insertWidth(sFmt, nFWidth, nPWidth)
  insertWidth = Replace(Replace(sFmt, "@F", nFWidth), "@P", nPWidth)
End Function

' stolen from http://stackoverflow.com/a/11262441/603855
Class cFormat
  Private m_oSB
  Private Sub Class_Initialize()
    Set m_oSB = CreateObject("System.Text.StringBuilder")
  End Sub ' Class_Initialize
  Public Function formatOne(sFmt, vElm)
    m_oSB.AppendFormat sFmt, vElm
    formatOne = m_oSB.ToString()
    m_oSB.Length = 0
  End Function ' formatOne
  Public Function formatArray(sFmt, aElms)
    m_oSB.AppendFormat_4 sFmt, (aElms)
    formatArray = m_oSB.ToString()
    m_oSB.Length = 0
  End Function ' formatArray
End Class ' cFormat

出力:

========================================
PDF file name            Number of pages
----------------------------------------
xpl.txt                              706
1.abc                                534
xpl.vbs                              580
1.txt                                290
xx.txt                               302
----------------------------------------
Total                               2412
========================================
于 2013-01-29T11:03:06.930 に答える