code改进计算PDF文件的网页 [英] Code improvements for counting the pages of PDF files
本文介绍了code改进计算PDF文件的网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能获得总页数均 PDF
进入该文件夹的价值?我还要说的输出被写入 .TXT
文件。
所以,我需要知道我怎么可以得到所有 PDF
和方式的页面的数量在一列把所有的网页数和 PDF
的名字。
我想这样的输出:
页的PDF文件名编号
-------------------------------------
firstpdffile 30页
secondpdffile 25页
thirdpdffile 10页
fourthpdffile 5页
-------------------------------------
共70页
显式的选项
专用功能getPdfPgCnt(BYVAL SPATH)返回传递路径文件的页数
昏暗的strTStr
随着的CreateObject(的ADODB.Stream)
。打开
.Charset =X-ANSI
.LoadFromFile SPATH
strTStr = .ReadText(-1)
结束与
随着(新正则表达式)
.Pattern =类型\ S * /页[^ S]
.IgnoreCase = TRUE
。全球= TRUE
getPdfPgCnt = .Execute(strTStr).Count之间
结束与
如果getPdfPgCnt = 0然后getPdfPgCnt = 1
端功能
--------------------------------
朦胧OFSO,iFile的
设置OFSO =的CreateObject(Scripting.FileSystemObject的)
枚举VBS的基本目录的PDF文件
对于每个iFile的在oFso.getFolder(oFso.GetParentFolderName(WScript.ScriptFullName))。文件
如果LCASE(oFso.GetExtensionName(iFile的))=PDF然后WScript.Echo iFile的&放大器; 有&放大器; getPdfPgCnt(iFile的)及页面。
下一个
设置OFSO =什么
--------------------------------
我用这个蝙蝠
文件来运行脚本
关闭@echo
颜色0A
@set currentdir =%CD%
标题的PDF页面计数器
德尔temp1.txt
德尔temp2.txt
德尔temp3.txt
德尔output.txt的
CSCRIPT pdfpagecount.vbs> temp1.txt
BatchSubstitute.bat%CD%temp1.txt> temp2.txt和放大器; BatchSubstitute.bat\temp2.txt> temp3.txt和放大器;键入temp3.txt | FINDSTR / I / V / C:微软>> output.txt的&放大器;德尔temp1.txt和放大器;德尔temp2.txt和放大器;德尔temp3.txt
出口
关闭@echo
REM - prepare命令处理器 -
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
:: BatchSubstitude - 通过在线分析文件行并替换子串
::语法:BatchSubstitude.bat OldStr中newstr文件
:: OldStr [中] - 串要被替换
::中newstr [中] - 字符串替换
::文件[中] - 文件被解析
:$改变20100115
:$源http://www.dostips.com
如果%〜1==FINDSTR^ ::%〜F0&放大器; GOTO:EOF
FOR / F令牌= 1 * delims =]%% A有('类型%3 |找到/ N / V,)做(
一套行= %% B
如果定义行(
呼叫建立行=回声%%行:%〜1 =%〜2 %%
在('回声。%% %%行)做%%〜x对于/ Fdelims =%% X
)ELSE回声。
)
解决方案
努力要把精力放在
- 在表(头,身,尾),如输出控制台脚本
- 在总结值
您需要什么
- 在.NET中的sprintf的格式化(类cFormat)
- 总和变量初始化为0,在循环之前,在循环之后将在循环中,输出/使用
在code:
'14490628.vbs
http://stackoverflow.com/questions/14490628/$c$c-improvements-for-counting-the-pages-of-pdf-files
显式的选项
昏暗OFS:设置OFS =的CreateObject(Scripting.FileSystemObject的)
昏暗SDIR:SDIR =.. \ .. \ 10041057 \ DATA
昏暗oFmt用来:设置oFmt用来=新cFormat
昏暗的nFWidth:nFWidth = 25'的文件名
昏暗的nPWidth:nPWidth = 15'的页数
昏暗的nLWidth:nLWidth = nFWidth + nPWidth的整条生产线
昏暗sRuler1:sRuler1 =字符串(nLWidth,=)
昏暗sRuler2:sRuler2 =字符串(nLWidth, - )
昏暗sFmtT:sFmtT = insertWidth({4} {2} {0, - @ F} {1,@ P} {2} {3},nFWidth,nPWidth)
昏暗的aDataT:aDataT =阵列(PDF文件名,页数,vbCrLf,sRuler2,sRuler1)
昏暗的sFmtF:sFmtF = insertWidth({0, - @ F} {1,@ P},nFWidth,nPWidth)
暗淡aDataF:aDataF =阵列(,0)
昏暗的sFmtS:sFmtS = insertWidth({3} {2} {0, - @ F} {1,@ P} {2} {4},nFWidth,nPWidth)
昏暗的aDataS:aDataS =阵列(合计,0,vbCrLf,sRuler2,sRuler1)
WScript.Echo oFmt.formatArray(sFmtT,aDataT)
昏暗的OFILE
对于每个OFILE在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)
下一个
WScript.Echo oFmt.formatArray(sFmtS,aDataS)
功能getPdfPgCnt(sFSpec)
从VBScript的文档被盗
昏暗的下界:下界= 1
昏暗上界:上界= 1000
getPdfPgCnt = INT((上界 - 下界+ 1)* Rnd函数+下界)
端功能
功能insertWidth(sFmt,nFWidth,nPWidth)
insertWidth =替换(替换(sFmt,@F,nFWidth),@P,nPWidth)
端功能
从http://stackoverflow.com/a/11262441/603855被盗
类cFormat
私人m_oSB
私人小组Class_Initialize()
设置m_oSB =的CreateObject(System.Text.StringBuilder)
结束小组Class_Initialize
公共功能formatOne(sFmt,VELM)
m_oSB.AppendFormat sFmt,VELM
formatOne = m_oSB.ToString()
m_oSB.Length = 0
端功能formatOne
公共功能formatArray(sFmt,aElms)
m_oSB.AppendFormat_4 sFmt,(aElms)
formatArray = m_oSB.ToString()
m_oSB.Length = 0
端功能formatArray
末级cFormat
输出:
====================================== ==
页PDF文件名编号
----------------------------------------
xpl.txt 706
1.abc 534
xpl.vbs 580
1.TXT 290
xx.txt 302
----------------------------------------
共有2412
========================================
How can I get the value of the total pages of all PDF
into the folder? I would also that the output is written into a .txt
file.
So, I need to know how can I get the number of the pages of all PDF
and a way to put in a column all the pages numbers and the PDF
names.
I would an output like this:
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
'--------------------------------
I use this .bat
file to run the script
@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.
)
解决方案
Trying to concentrate on
- table (header, body, footer) like output for console scripts
- summing up values
What you need
- .NET sprintf like formatting (class cFormat)
- sum variable initialized to 0 before the loop, adding in the loop, output/use after the loop
In code:
' 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
output:
========================================
PDF file name Number of pages
----------------------------------------
xpl.txt 706
1.abc 534
xpl.vbs 580
1.txt 290
xx.txt 302
----------------------------------------
Total 2412
========================================
这篇关于code改进计算PDF文件的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文