Jump to content

Script to grab last lines of a file


 Share

Recommended Posts

Hello -

I have just discovered AutoIT and it looks great!

Here that the library that I work at we need a system to meter printing for our patrons. Quite some time ago I wrote one using vbscript and gooeyscript and it worked after a fashion, but it was a little slow and hard to work with. Now my boss wants me to resurrect it as the product that we bought to do the job (PCCop) is unstable and inaccurate. I thought that I would try to use AutoIT for it.

The system worked like this: People print, and the printer is set up at each patron station to use the great redmon port redirector program. The printer is set up to use a post script driver. Redmon redirects the printer output to the vbscript, and the vbscript finds in the postscript data stream the number of pages in the print job. This is usually found in the last 4-5 lines of the ps file.

I have a start on a little autoit script to save the ps file, based on the consoleread.au3 example:

CODE

#Include <File.au3>

$path = "C:\PrintHandler\"

$ps_file_path = $path & "postscript\"

$ps_file = _TempFile($ps_file_path, "", ".ps", 7)

$file = FileOpen($ps_file, 1)

While True

$data = ConsoleRead()

FileWriteLine($file, $data)

If StringInStr($data, "%%EOF") Then ExitLoop

WEnd

FileClose($file)

ShellExecute ($path & "display_print_jb.exe",$ps_file)

Exit

This script will call another one, that will calculate the pages, cost, and ask the patron if he/she indeed wants to print. If they do agree, the script will pscp (part of putty for you non-unix types) the file to a linux server where the file will wait until the patron pays, then it will be printed.

This little script does work (please feel free to comment on it and offer improvements!), but what I need to do is be able to get the number of pages in the print job, either from the data stream as it passes through this script, or in the script that this will call. With the old VBscript, the data seemed to come in one line at a time, so I was able to simply look for things like "%%Pages: (number of pages)" and "%%BeginNonPPDFeature: NumCopies " in each line and I was set. With AutoIT, it appears that the ps data comes into the script in one chunk. I was not able to read one line at a time.

Is there a function to retrieve just a certain portion of a file? Or would I be better off using vbscript at least for the postscript capture part of this system?

Thanks!

Library Mark

Link to comment
Share on other sites

Hello -

I have just discovered AutoIT and it looks great!

Here that the library that I work at we need a system to meter printing for our patrons. Quite some time ago I wrote one using vbscript and gooeyscript and it worked after a fashion, but it was a little slow and hard to work with. Now my boss wants me to resurrect it as the product that we bought to do the job (PCCop) is unstable and inaccurate. I thought that I would try to use AutoIT for it.

The system worked like this: People print, and the printer is set up at each patron station to use the great redmon port redirector program. The printer is set up to use a post script driver. Redmon redirects the printer output to the vbscript, and the vbscript finds in the postscript data stream the number of pages in the print job. This is usually found in the last 4-5 lines of the ps file.

I have a start on a little autoit script to save the ps file, based on the consoleread.au3 example:

#Include <File.au3>
$path = "C:\PrintHandler\"
$ps_file_path = $path & "postscript\"
$ps_file  = _TempFile($ps_file_path, "", ".ps", 7)
$file = FileOpen($ps_file, 1)

While True
    $data = ConsoleRead()
    FileWriteLine($file, $data)
    If StringInStr($data, "%%EOF") Then ExitLoop
WEnd

FileClose($file)
ShellExecute ($path & "display_print_jb.exe",$ps_file)
Exit

This script will call another one, that will calculate the pages, cost, and ask the patron if he/she indeed wants to print. If they do agree, the script will pscp (part of putty for you non-unix types) the file to a linux server where the file will wait until the patron pays, then it will be printed.

This little script does work (please feel free to comment on it and offer improvements!), but what I need to do is be able to get the number of pages in the print job, either from the data stream as it passes through this script, or in the script that this will call. With the old VBscript, the data seemed to come in one line at a time, so I was able to simply look for things like "%%Pages: (number of pages)" and "%%BeginNonPPDFeature: NumCopies " in each line and I was set. With AutoIT, it appears that the ps data comes into the script in one chunk. I was not able to read one line at a time.

Is there a function to retrieve just a certain portion of a file? Or would I be better off using vbscript at least for the postscript capture part of this system?

Thanks!

Library Mark

Just add that to the While/WEnd loop you already have. Check for those values the same way you are checking for "%%EOF" in the loop.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi LibraryMark,

1st Welcome to the AutoIt Forums! :idiot:

Some of the following tips may not apply to you, but it may make your life a bit easier here on the forum in the future.

CODE
  • Did you know that we have an awesome search feature?

    You can find many answers to your current questions, just by typing in the right search patterns.

  • A suggestion is to use the Advanced Search mode:

  • Type your specific search term in quotes.
  • Click the forum you want to search in (the one most likely to have your information would generally be the Example Script forum and or the General Help and Support Forum).
  • Click on "Search titles only" radio button.
  • Click perform search.
The above will help you narrow down your searches and prevent you from unneccesarily posting a new thread.

[*]Also, you should try to read the Sticky posts that are at the top of each of the AutoIt Forums you enter such as:

[*]Keep in mind, the help file will be your best friend, however you may find some of the tutorials written by some of our elite forum members helpful.

[*]Forum Etiquette:

  • Making a new thread:

    • Use the Search feature first to see if your question has already been answered.
    • Look in the help file as well before even thinking of posting (When what you want could be obtained by simply reading the help file, you don't generally get a good response from your AutoIt community).
    • Titles are very important here. 1 word titles or titles like "help me", "write something for me", "I'm a noob" etc... aren't tolerated.
    • Make sure you are posting in the correct forum:

      • General Help and Support:

        • This forum is for AutoIt related support questions only. If you have a question related to another language, or nothing at all to do with AutoIt then you need to post in the chat forum, or in that languages perspective forum.
      • Example Script:

        • This forum is for AutoIt scripts/executables only.
        • Source code is preferred but not necessary, you do have the right to just post the binary of your project if you wish.
        • Please don't post questions in this forum unless it's directly related to a thread already existing.
    • Use common sense when creating a new thread.

      Ask yourself if the title is descriptive enough to even interest someone (preferably those that know what they are talking about) to even look at your thread, let alone reply in it.

    • Think about how it would show in the search feature if someone were to look for something just like you are looking for (think of the keywords you used yourself and obviously didn't find anything (because we know you used the search feature :) ) and use those types of keywords in your title as well).
  • Thread content:

    • Be descriptive with your query. (Make sure we actually know what you want to do).
    • Show you've made an effort in coding what you want (provide the reproducer code (generally no more than 50 lines as people lose interest in debugging someones script for free)).
    • Don't talk in ebonics. A lot of the forum members are adults, and a lot of them know how to help you, but talk like a child, you'll be treated as such.
    • Don't ask for help making keyloggers, spam (even if it's to do as a prank), or anything that can be thought of as malicious. You'll more than likely have the thread locked by a moderator, and take a bashing from your fellow AutoIt community.
    • When posting code, use code boxes. This can be accomplished by using [code ]<content here>[/code ] (No spaces between the brackets []).

      Using code boxes will keep the indentation and make it easier to read for others to help you.

  • Bumping your threads:

    • Use common courtesy here.

      Keep in mind every time you bump your thread to the top of the forum, you knock the other threads down a notch.

      Everyone posting for help has just as much right for their threads to get read as you do.

      Because of that, do not bump your post more than once in a 24 hour period.

      A Bump is simply posting in your thread with nothing that pertains to your query with the sole purpose of moving it up.

      Deleting previous bumps, and posting new ones is not tolerated, and the moderators can find those deletions, so do yourself a favor and don't cross that line >_< .

  • Rude or obnoxious content:

    This falls pretty much under the common sense thing. If you use it (common sense) before posting, you won't have issues.

    • Don't use foul language, remember, a lot of the community is at work when they read these threads.
    • Don't provoke or instigate an argument with someone.
  • Double Posting:

    • It's understood that sometimes there's a lag in the system, and sometimes people don't see their post go up right aways so they post again.

      If this happens to you, simply notify a moderator with the report feature in the post, and politely ask them to delete it.

    • If you're just creating another topic because your original topic is not being answered the way you want or at all, this is not tolerated. You could lose your posting privileges all together over it.
  • Non-English languages

    • If English is not your primary language, please make an attempt to interpret (yourself or online) and post that interpretation.

      We have wonderful users from around the world, so after you've done your post in English, back it up with your question also in your native tongue (You may find your answer much quicker using both).

That's it for now, I hope you have a wonderful learning experience, and hope to see you contribute to the community as your knowledge grows.
Link to comment
Share on other sites

Just add that to the While/WEnd loop you already have. Check for those values the same way you are checking for "%%EOF" in the loop.

:)

Thanks for the reply. When I make the loop:

While True
    $data = ConsoleRead()
    FileWriteLine($file, $data)
    If StringInStr($data, "%%EOF") Then ExitLoop
    if StringInStr($data, "%%Pages:") then 
        MsgBox (0, "PS file", "Found it: " & $data)
    EndIf
WEnd

I get a message box with the contents of the entire ps file, not just the line I am looking for. Why am I not seeing just one line?

Link to comment
Share on other sites

Thanks for the reply. When I make the loop:

While True
    $data = ConsoleRead()
    FileWriteLine($file, $data)
    If StringInStr($data, "%%EOF") Then ExitLoop
    if StringInStr($data, "%%Pages:") then 
        MsgBox (0, "PS file", "Found it: " & $data)
    EndIf
WEnd

I get a message box with the contents of the entire ps file, not just the line I am looking for. Why am I not seeing just one line?

I don't know where "consoleread.au3" came from, but(Got it; you meant the example script from the help file.) [T]hat's not how AutoIt's native ConsoleRead() works. The native function returns all the STDIN data since the last call. If that is multiple lines, it's on you to figure that out and deal with it. You could easily get a StringRegExp() to pull just the value you are interested in.

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I don't know where "consoleread.au3" came from, but(Got it; you meant the example script from the help file.) [T]hat's not how AutoIt's native ConsoleRead() works. The native function returns all the STDIN data since the last call. If that is multiple lines, it's on you to figure that out and deal with it. You could easily get a StringRegExp() to pull just the value you are interested in.

:)

Thanks again for the reply. StringRegExp() looks complicated as hell. It's going to take me a while to wrap my brain around that. Just so that I know I am heading in the right direction, I can use that to search for a pattern and return a string that is offset by some number of characters from the searched for string? 'Cause that is what I am going to have to do: Find "%%Pages: " and return what is about 3-4 characters later in the file.

Later -

Library Mark

Edited by LibraryMark
Link to comment
Share on other sites

Thanks again for the reply. StringRegExp() looks complicated as hell. It's going to take me a while to wrap my brain around that. Just so that I know I am heading in the right direction, I can use that to search for a pattern and return a string that is offset by some number of characters from the searched for string? 'Cause that is what I am going to have to do: Find "%%Pages: " and return what is about 3-4 characters later in the file.

Later -

Library Mark

Regular expressions are a very powerful language of their own. It can do things like return all characters after "%%Pages" but before an end of line, very easily. If you are not up to learning it right now, you can just use StringInStr() to get the offset, then use StringMid() to pull what follows it, etc.

RegExp is a popular topic around here because many of us are not "real" programmers and we like having the opportunity to learn such a powerful tool (and gain geek-points). So lots of help is available. Here's an example:

; Simulated data:
$data = "Long string to simulate data read from ConsoleRead(), with" & @CRLF & _
        "multiple lines and lots of garbage in it: %%%!!!<> test test% %% %%% %%%%" & @CRLF & _
        "Some final garbage... and then: %%Pages: 123456%%EOF"
        
If StringInStr($data, "%%Pages: ") Then
    $avPages = StringRegExp($data, "(?:%%Pages: )(\d+)", 1)
    If Not @error Then
        MsgBox(64, "Match", "Got a match: " & $avPages[0])
    Else
        MsgBox(16, "No Match", "@error = " & @error)
    EndIf
EndIf

The pattern used in the RegExp says "any number of 1 or more digits (0-9) that come immediately after "%%Pages: ". We don't want the string "%%Pages: " to be part of the output, so that is put in a non-capturing group, indicated by "?:".

...another notch in the hilt of my +2 Keyboard of Geekiness.

:)

Edit: Tweaked to include space after "%%Pages:".

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...