Jump to content
Sign in to follow this  

what's wrong with my code (count pages of a postscript files)

Recommended Posts


postscript files 3 sprecifics lines :

%%Pages: (atend) //i want to ignore this one

%%Pages: 4 // i want extract the number (here : 4)

userdict /#copies 50 put (here : 50)

but my following codes seems doesn't work and don't know why.

can you help me ?

dim $PostscriptFile='20121123172655.ps'
dim $copies=1
dim $pages=1
$file = FileOpen($PostscriptFile, 0)
If $file = 0 Then
    MsgBox(0, "Error", "Unable to open file.")
; Read in lines of text until the EOF is reached

While 1
   Local $line = FileReadLine($file) 
If @error = -1  Then ExitLoop
if StringInStr($line,"%%Pages:",1,2) >0 then
         $array1= StringRegExp($line,"%%Pages:\h\(d+)",3)
      $pages = Number($array1[0])
   if StringInStr($line,"/#copies",1,1)>0  then
     $array2= StringRegExp($line,"/#copies\h\(d+)",3)
     $copies = Number($array2[0])
 MsgBox(0,"total pages",$copie * $pages)

thx a lot

Share this post

Link to post
Share on other sites

Might be that the regex is wrong, but try it like this.




MsgBox(0,"Line read:",$line)



Share this post

Link to post
Share on other sites

In your regexp patterns, are misplaced.


Local $PostscriptFile = '20121123172655.ps'
Local $aRes, $copies = 1, $pages = 1
$file = FileRead($PostscriptFile)
If Not @error Then
$aRes = StringRegExp($file, "(?i)%%Pages:h*(d+)", 1)
If Not @error Then
$pages = Number($aRes[0])
;~ Else
;~ ConsoleWrite("%%Pages not found" & @LF)
$aRes = StringRegExp($file, "(?i)/#copiesh*(d+)", 1)
If Not @error Then
$copies = Number($aRes[0])
;~ Else
;~ ConsoleWrite("/#copies not found" & @LF)
MsgBox(0,"Total pages", $copies * $pages)
ConsoleWrite("Unable to read file " & $PostscriptFile & @LF)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...