Sign in to follow this  
Followers 0
BPBNA

Parsing a file

14 posts in this topic

Hi,

I'm trying to write a script that parses a text file that is generated by another script of mine. I can't seem to get it working. My script doesn't do too much so far, but I want to get this working before I go any farther.

#include <GUIConstants.au3>

Opt("TrayIconDebug", 1)

Dim $name[50], $phone[50], $ext[50], $location[50]
Dim $issue[50][1000]
Dim $count = 0, $block = 0, $linenum = 1

$file = FileOpen($CmdLineRaw, 0)
$line = FileReadLine($file, $linenum)
$readerror = @error

While $readerror <> -1
    If StringInStr("Name: ", $line) <> 0 then
        $name[$count] = StringTrimLeft($line, 6)
    EndIf
    If StringInStr("Phone Number: ", $line) <> 0 then
        $phone[$count] = StringTrimLeft($line, 14)
    EndIf
    If StringInStr("Ext: ", $line) <> 0 then
        $ext[$count] = StringTrimLeft($line, 5)
    EndIf
    If StringInStr("Location: ", $line) <> 0 then
        $location[$count] = StringTrimLeft($line, 10)
    EndIf
    If StringInStr("Issue: ", $line) <> 0 then
        $line = FileReadLine($file, $linenum)
        $readerror = @error
        While $readerror <> -1
            If $line <> "" then
                $issue[$count][$block] = StringTrimLeft($line, 8)
                $block = $block + 1
                $linenum = $linenum + 1
                $line = FileReadLine($file, $linenum)
                $readerror = @error
            Else
                ExitLoop
            EndIf
        WEnd
    EndIf
    $count = $count + 1
    $linenum = $linenum + 1
    $line = FileReadLine($file, $linenum)
    $readerror = @error
WEnd


$parsed = GUICreate("File Information", 600, 300)
$issuefield = GUICtrlCreateEdit ("", 176,32,121,97,$wS_DISABLED+$WS_VSCROLL)
GUISetState(@SW_SHOW, $parsed)

For $x = 0 to (Ubound($issue, 2) - 1)
    GUICtrlSetData($issuefield, $issue[0][$x], 1)
Next

While 1
    $msg = GUIGetMsg()

    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd

When I check what the $file variable is, it just says 0, I don't think thats normal for a FileOpen return value is it?

Also, I'm not sure how to use UBound to find the size of the second dimension of $issue for each of the first dimensions since they will all be different lengths.

One more thing I was wondering... Is there any way to declare a array but without a set size so I dont have to just put 50 or 1000?

This is the file I am trying to parse:

closed_2006_07_04.txt

Share this post


Link to post
Share on other sites



You might be better off with _FileReadToArray() or even StringRegExp(), but what happens when you do debug like this?

$file = FileOpen($CmdLineRaw, 0)
If @error Then MsgBox(64, 'Info', 'Error ' & $CmdLineRaw)


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

$CmdLineRaw comes out fine. I pass the file I want to parse into this program with something like this:

$fileopn = FileOpenDialog("Choose file...",@ScriptDir,"Text (*.txt)")
Run("parser.exe " & $fileopn)

If I do try your suggestion to debug, I don't get the MsgBox. It isnt erroring out, its just setting it to 0 for some reason.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

$CmdLineRaw comes out fine. I pass the file I want to parse into this program with something like this:

$fileopn = FileOpenDialog("Choose file...",@ScriptDir,"Text (*.txt)")
Run("parser.exe " & $fileopn)

If I do try your suggestion to debug, I don't get the MsgBox. It isnt erroring out, its just setting it to 0 for some reason.

Does parser.exe use the command line parameters to do its thang:

MsgBox(64, "Debug Point", "Input command line param 1 = " & $CmdLine[1]
$file = FileOpen($CmdLine[1], 0)
MsgBox(64, 'Debug Point', '@error  = ' & @error & '  $file = ' & $file)

:D

Edit: Tweaked to avoid reserved variable $CmdLineRaw

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

Share this post


Link to post
Share on other sites

I can't use $CmdLine[1] because the path has spaces(Documents and Settings) so it interprets it as seperate parameters. $CmdLineRaw has to be used so it uses the entire path to the file. It does come out the way it should. The big block of code in my first post is the parser.exe.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I can't use $CmdLine[1] because the path has spaces(Documents and Settings) so it interprets it as seperate parameters. $CmdLineRaw has to be used so it uses the entire path to the file. It does come out the way it should. The big block of code in my first post is the parser.exe.

My mistake, I should not have used $CmdLineRaw as variable, but you CAN use command line parameters with spaces, just put doublequotes around them:

Run('parser.exe "' & $fileopn & '"')oÝ÷ Ù8^§zË"·­Mú
gKwµ{"uêZ®Ç«{²+b³Z·*.j·©jË"h)^:§Ë(ëax%GºÚ"µÍÌÍÙ[HH[[    ][ÝÜÙ^HÎÌLÑØÝ[Y[È[Ù][ÜÉÌLÓ^UÙÌLÓ^Q[K   ][ÝË

Because I believe $CmdLineRaw is the entire commandline, including the script name.

:D

Edit: Tweak example for clarity.

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

Share this post


Link to post
Share on other sites

You want 'parser.exe "C:\Documents and Settings\...\My Documents\MyFile.txt"' just to be safe. Add double quotes in any case so that you don't have to worry about certain files going wrong.

If you are dragging and dropping MyFile.txt on to parser.exe, Windows will double-quote it for you.

Share this post


Link to post
Share on other sites

$CmdLineRaw does not return the command, only the parameters. The help file says:

So if your script is run like this:

AutoIt3.exe myscript.au3 param1 "this is another param"

$CmdLineRaw equals... myscript.au3 param1 "this is another param"

Anyways, I'm still wondering about the _FileReadToArray.... since my text file will have an indefinite amount of lines for the issue, how would I get it to figure out where that ends?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

$CmdLineRaw does not return the command, only the parameters. The help file says:

Anyways, I'm still wondering about the _FileReadToArray.... since my text file will have an indefinite amount of lines for the issue, how would I get it to figure out where that ends?

That's exactly where I got the idea that the executed script name was included in $CmdLineRaw. From the help file on Command Line Parameters (bold is mine):

In addition to $CmdLine there is a variable called $CmdLineRaw that contains the entire command line unsplit, so for the above example:

$CmdLineRaw equals... myscript.au3 param1 "this is another param"

Anyway, I did the only sensible thang and tested it with Test1.au3 and Test2.au3 (compiled as Test2.exe):

; Test1.au3 calling other script
Run(@ScriptDir & '\Test2.exe /x "Test Param (text)"')
MsgBox(64, "Launched", "Launched Test2.au3.")oÝ÷ Ù«­¢+ØìQÍÐȹÔÌ¥ÍÁ±äÁɵÑÉÌÉ¥Ù)5Í   ½à ØаÅÕ½ÐíQÍÐÈÅÕ½Ðì°ÅÕ½ÐíI¥ÙÀÌØí
µ1¥¹IÜôÅÕ½ÐìµÀìÀÌØí
µ1¥¹IܵÀì
I1µÀì|($$ÅÕ½ÐìÀÌØí
µ1¥¹lÅtôÅÕ½ÐìµÀìÀÌØí
µ1¥¹lÅtµÀì
I1µÀì|($$ÅÕ½ÐìÀÌØí
µ1¥¹lÉtôÅÕ½ÐìµÀìÀÌØí
µ1¥¹lÉt

That worked fine, and you were right, $CmdLineRaw has only the parameters. But the parameters with quotes around them work fine too.

On _FileReadToArray(): You should get the line count in $aArray[0]. So the last line would be in $aArray[$aArray[0]].

:D

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

Share this post


Link to post
Share on other sites

myscript.au3 is a parameter passed onto AutoIt3.exe (the current interpreter as being uncompiled). $CmdLineRaw is a dirty way to check incoming parameters for your script.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

myscript.au3 is a parameter passed onto AutoIt3.exe (the current interpreter as being uncompiled). $CmdLineRaw is a dirty way to check incoming parameters for your script.

I see what you're saying. My test above didn't show the script name in $CmdLineRaw because Test2.au3 was compiled into Test2.exe, and therefore the script name was not a parameter passed to AutoIT3.exe.

Just seems that the purpose of a help file dictates that such a difference should be made much clearer...

Perhaps like this:

The special array $CmdLine is initialized with the command line parameters passed in to your AutoIt script. Note the scriptname is not classed as a parameter; get this information with @ScriptName instead. A parameter that contains spaces must be surrounded by "double quotes". Compiled scripts accept command line parameters in the same way.

$CmdLine[0]is number of parameters

$CmdLine[1] is param 1 (after the script name)

$CmdLine[2] is param 2 etc.

$CmdLine[$CmdLine[0]] is one way to get the last parameter...

In addition to the $CmdLine array, there is a variable called $CmdLineRaw that contains all parameters from the command line unsplit. If parameters are passed to a compiled script, then $CmdLineRaw will contain in one string all the same parameters that will be in the $CmdLine array.

So if your compiled script is run like this:

myscript.exe param1 "this is another param"

The parameters will look like this:

$CmdLine[0] = 2

$CmdLine[1] = param1

$CmdLine[2] = this is another param

@ScriptName = myscript.exe

$CmdLineRaw = param1 "this is another param"

The result is slightly different if passing an uncompiled script to AutoIT3.exe, because the script name gets included in $CmdLineRaw. So if run like this:

AtuoIt3.exe myscript.au3 param1 "this is another param"

The parameters will look like this:

$CmdLine[0] = 2

$CmdLine[1] = param1

$CmdLine[2] = this is another param

@ScriptName = myscript.au3

$CmdLineRaw = myscript.au3 param1 "this is another param"

Just a nit at which to pick... :D

Edit: Verbiage tweak.

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

Share this post


Link to post
Share on other sites

I see what you're saying. My test above didn't show the script name in $CmdLineRaw because Test2.au3 was compiled into Test2.exe, and therefore the script name was not a parameter passed to AutoIT3.exe.

Just seems that the purpose of a help file dictates that such a difference should be made much clearer...

Perhaps like this:

Just a nit at which to pick... :D

Edit: Verbiage tweak.

You should also know that /ErrorStdOut and /AutoItExecuteScript can also be part of $CMDLINERAW when the script is executed uncompiled. This is why I stated as being a dirty method to use $CMDLINERAW as you need to filter this extra content out to get just the parameters that your script can use.

Unfortunate that $CMDLINERAW displays different uncompiled as to being compiled but all it is doing is returning the parameters as sent to the interpreter (not the script).

Share this post


Link to post
Share on other sites

You should also know that /ErrorStdOut and /AutoItExecuteScript can also be part of $CMDLINERAW when the script is executed uncompiled. This is why I stated as being a dirty method to use $CMDLINERAW as you need to filter this extra content out to get just the parameters that your script can use.

Unfortunate that $CMDLINERAW displays different uncompiled as to being compiled but all it is doing is returning the parameters as sent to the interpreter (not the script).

That makes perfect sense. For an uncompiled script you get everything that follows AutoIt3.exe, including switches and parameters intended only for the interpreter, not the script. I vote for a tweak to the help files, under Command Line Parameters section, that makes this all clear(er). Thanks for the education on the point!

:D


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

Share this post


Link to post
Share on other sites

Turns out using an ini file to pass the file name and to read the actual data was much easier for me. My program works perfect now :D

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  
Followers 0