Jump to content

FileReadToArray


Recommended Posts

I wanted to avoid including another UDF in myscript and I've got the code from this function.

The problem is : the code doesn't work (always the array returned has 1 element) and the function _FileReadToArray works ... and I can't see any difference.

here is my code:

$tmpfile = FileOpen("C:\TFTP Data\tmp_log12345.txt", 0)
    $aArray = StringSplit(StringStripCR(FileRead($tmpfile, FileGetSize("C:\TFTP Data\tmp_log12345.txt"))), @LF)
FileClose($tmpfile)

and here is using the UDF

Local $aArray[30]
    _FileReadToArray("C:\TFTP Data\tmp_log12345.txt", $aArray)

I cannot find any difference between them ... but maybe I'm blind right now :rolleyes:

and another weird thing ... this code works on a separate test file and fails when included in the main script

... sorry for not posting more code: reasons: huge code and using a terminal-like software.

Thanks

EDIT:

Got it to work finally ... (remembered about not mixing file handles and filenames ..)

removed FileOpen and FileClose statements and replaced the $tmpfile with the filename

$aArray = StringSplit(StringStripCR(FileRead("C:\TFTP Data\tmp_log12345.txt", FileGetSize("C:\TFTP Data\tmp_log12345.txt"))), @LF)
Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

This is the content of the txt file:

* Summit200-24:8 # show snmp community

Community Index  : private
Community Name   : private
Security Name   : v1v2c_rw
Context EngineID : 80:00:07:7c:03:00:04:96:27:18:0b 'H'
Context Name     :
Transport Tag   : 
Storage Type     : Permanent
Row Status     : Active

Community Index  : public
Community Name   : public
Security Name   : v1v2c_ro
Context EngineID : 80:00:07:7c:03:00:04:96:27:18:0b 'H'
Context Name     :
Transport Tag   : 
Storage Type     : Permanent
Row Status     : Active

Total num. of entries in snmpCommunityTable : 2
* Summit200-24:9 #

and here is the function which doesn't work:

Func snmpClick()
    Local $startline = _GetLogLastLineNumber ($tterm_log)
    _SendTeraTerm($tt_hwnd, "show snmp community")
    _WaitPrompt("#", 10000, $delay, $tterm_log)
    Local $endline = _GetLogLastLineNumber ($tterm_log)
    Local $tmpfile = FileOpen("C:\TFTP Data\tmp_log12345.txt", 2)
    For $k=$startline To $endline
        Local $line = FileReadLine($tterm_log, $k)
        FileWriteLine ($tmpfile,$line)
    Next
    FileClose ($tmpfile)
    Sleep (500)
    $tmpfile = FileOpen("C:\TFTP Data\tmp_log12345.txt", 0)
    $aArray = StringSplit(StringStripCR(FileRead($tmpfile, FileGetSize("C:\TFTP Data\tmp_log12345.txt"))), @LF)
    FileClose ($tmpfile)
    MsgBox (0, "[0]", $aArray[0])   ;just for test
    For $k=1 to $aArray[0]
        If StringInStr($aArray[$k], "Community Name") Then
            $snmp_name1 = StringMid ($aArray[$k], 20)
            MsgBox (0, "name1", $snmp_name1);test
            $snmp_type = StringMid ($aArray[$k+1], 20)
            MsgBox (0, "type", $snmp_type)  ;test
            Select
                Case $snmp_type = "v1v2c_rw"
                    $comm_rw = $snmp_name1
                Case $snmp_type = "v1v2c_ro"
                    $comm_ro = $snmp_name1
                Case Else 
                    update_status(" @ SNMP Community Name ERROR")
                    SetError(1)
                    Return
            EndSelect
        EndIf
    Next
    MsgBox (0, "RW", $comm_rw)  ;test
    MsgBox (0, "Ro", $comm_ro)  ;test
EndFunc

and the working one:

Func snmpClick()
    Local $startline = _GetLogLastLineNumber ($tterm_log)
    _SendTeraTerm($tt_hwnd, "show snmp community")
    _WaitPrompt("#", 10000, $delay, $tterm_log)
    Local $endline = _GetLogLastLineNumber ($tterm_log)
    Local $tmpfile = FileOpen("C:\TFTP Data\tmp_log12345.txt", 2)
    For $k=$startline To $endline
        Local $line = FileReadLine($tterm_log, $k)
        FileWriteLine ($tmpfile,$line)
    Next
    FileClose ($tmpfile)
    Sleep (500)
    Local $aArray[30]
    _FileReadToArray("C:\TFTP Data\tmp_log12345.txt", $aArray)
    For $k=1 to $aArray[0]
        If StringInStr($aArray[$k], "Community Name") Then
            $snmp_name1 = StringMid ($aArray[$k], 20)
            MsgBox (0, "name1", $snmp_name1);test
            $snmp_type = StringMid ($aArray[$k+1], 20)
            MsgBox (0, "type", $snmp_type)  ;test
            Select
                Case $snmp_type = "v1v2c_rw"
                    $comm_rw = $snmp_name1
                Case $snmp_type = "v1v2c_ro"
                    $comm_ro = $snmp_name1
                Case Else 
                    update_status(" @ SNMP Community Name ERROR")
                    SetError(1)
                    Return
            EndSelect
        EndIf
    Next
    MsgBox (0, "RW", $comm_rw)  ;test
    MsgBox (0, "Ro", $comm_ro)  ;test
EndFunc

It is somehow weird but ... the solution I've found is to replace the file handle with the filename.

As you can see - the lines relevant to this matter were the lines I've put in the original post, the remaining is "gibberish" :rolleyes:

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

I don't understand because the following script with your test file produce identical array

include <File.au3>

    $tmpfile = FileOpen("test.txt", 0)
    $aArray = StringSplit(StringStripCR(FileRead($tmpfile, FileGetSize("test.txt"))), @LF)
    FileClose ($tmpfile)
 
     Local $sArray[30]
    _FileReadToArray("test.txt", $sArray)
    
    $n=_ArrayComp($aArray, $sArray)
    if $n >0 Then
        msgbox(0, " array identical " & @AutoItVersion,"n=" & $n)
    Else
        msgbox(0, " array DIFFERENT " & @AutoItVersion,"n=" & $n)
    EndIf
    
        
Func _ArrayComp($aArray1, $aArray2)
    if Ubound($aArray1) <> Ubound($aArray1) Then Return 0
    For $i= 0 to Ubound($aArray1)-1
        if $aArray1[$i] <> $aArray2[$i] Then Return -$i
        Next
    Return $i
EndFunc

Link to comment
Share on other sites

I don't know ... it might be related to something inside my script ...

It is not like a bug or something wrong within AutoIt ... I guess I'm simply unlucky :rolleyes:

Thank you for your help.

your "not_working" example, works out of the box. Maybe the content of the file tmp_log12345.txt is not what you expect it to be in certain cases.

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

I don't know ... it might be related to something inside my script ...

It is not like a bug or something wrong within AutoIt ... I guess I'm simply unlucky :rolleyes:

Thank you for your help.

Or you mix opening with handle and filename for the same file which is documented as not to be done. Certainly a time dependancy the sleep little bigger can help ...
Link to comment
Share on other sites

Or you mix opening with handle and filename for the same file which is documented as not to be done. Certainly a time dependancy the sleep little bigger can help ...

I don't think that's the problem for two reasons.

1.) He did close the handle before using FileRead() with the filename

2.) Even if you don't close the handle, there is no problem, see my code below.

Output:

Without FileClose:

$line1_handle: line 1

$line2_handle: line 2

$line1_name: line 1

With explicit FileClose:

$line1_handle: line 1

$line2_handle:

$line1_name: line 1

As you can see, only if you explicitly close the handle with FileClose() there is a problem. Using FileRead on the very same file, WHILE the handle is open, does not matter.

$filename = "test123.txt"
$filehandle = FileOpen(&#036Ù[[[YK
BÌÍÛ[LWÚ[HH[TXY[J   ÌÍÙ[Z[KJBÌÍÛæSöæÖRÒfÆU&VDÆæRb33c¶fÆVæÖRâb33c¶ÆæS%öæFÆRÒfÆU&VDÆæ ÀÌØí¥±¡¹±°È¤()
½¹Í½±]É¥Ñ ÅÕ½Ðí]¥Ñ¡½ÕÐ¥±
±½ÍèÅÕ½ÐìµÀì
LF)
ConsoleWrite("$line1_handle: " & $line1_handle [ÈÔBÛÛÛÛUÜ]][ÝÉÌÍÛ[[][ÝÈ  [È ÌÍÛ[LöæFÆRfײ5$Äb¤6öç6öÆUw&FRgV÷C²b33c¶ÆæSöæÖS¢gV÷C²fײb33í±¥¹Å}¹µµÀì
I1¤()¥±
±½Í ÀÌØí¥±¡¹±¤((ÀÌØí¥±¡¹±ôleOpen($filename, 0)
$line1_handle = FileReadLine($filehandlKJBÌÍÛ[LWÛ[YHH[TXY[J ÌÍÙ[[[YKJB[PÛÜÙJ  ÌÍÙ[æFÆR¢b33c¶ÆæS%öæFÆRÒfÆU&VDÆæRb33c¶fÆVæFÆRÃ" ¤6öç6öÆUw&FRÅÕ½Ðí]¥Ñ áÁ±¥¥Ð¥±
±½ÍèÅÕ½ÐìµÀì
I1¤)
½¹Í½±]É¥Ñ ÅÕ½ÐìÀÌØline1_handle: " & $line1_handle & @CRLF)
ConsoleWrite(&quÝÉÌÍÛ[[N    ][ÝÈ  [È ÌÍÛ[[H    [ÈÔBÛÛÛÛU&FRgV÷C²b33c¶ÆæSöæÖS¢gV÷C²fײb33c¶ÆæSöæÖRfײ5$Äb

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

I don't think that's the problem for two reasons.

1.) He did close the handle before using FileRead() with the filename

2.) Even if you don't close the handle, there is no problem, see my code below.

Output:

Without FileClose:

$line1_handle: line 1

$line2_handle: line 2

$line1_name: line 1

With explicit FileClose:

$line1_handle: line 1

$line2_handle:

$line1_name: line 1

As you can see, only if you explicitly close the handle with FileClose() there is a problem. Using FileRead on the very same file, WHILE the handle is open, does not matter.

[autoit]c2--><!--CiYjMDM2O2ZpbGVuYW1lID0gJnF1b3Q7dGVzdDEyMy50eHQmcXVvdDÒÒS]Ó^MÖ[ÖÚTÖÐNRQVÕÑÕRÐÖZQLÕVUÌ[Ð]ÒÔ[ÛR^^ØUVÄÕcõsV¶$uVu4$vwÅVÕf¤W&ÕVô¦×t×¥u¦ÖÇ5¥v&Õ'5¥7wµô´¦×t×¥v ¦ÇU¥µÑiMå%iÁYMi]­Q±ÕiMµ%éé9©Ñµ]á±µÑiMÝá-E½µ%éé9©ÑÍÕ±5°å½eÕ­UgPSBGaWxlUmVhZExpbm

oJiMwMzY7ZmlsZWhhbmRsZSwyKQoKQ29uc29sZVdyaXRlKCZxdW90OYÚYÔ[ÖSØÓÚP[XÖÙÒQÑÙÔQSVÚÓÕÛ[ÙÛXÖG6Ô¤¤æ§G6sVÄÕcõsV¶$uSd5§Es÷Õst÷Ô¤¤æ§G6sVÄÕcõsV¶$uVp¦ÔgFÍE9MQeÁ

­9Ù

Link to comment
Share on other sites

@enaiman,

As /dev/null says your not working script works out of the box: I created a "C:\TFTP Data" folder and put the copy of "tmp_log12345.txt" in it.

I then commented out all the lines in your function that had to do with creating "tmp_log12345.txt", that is all lines before "$tmpfile = FileOpen("C:\TFTP Data\tmp_log12345.txt", 0). Called your function and it worked as expected. So I support /dev/null's idea:

Maybe the content of the file tmp_log12345.txt is not what you expect it to be in certain cases.

Link to comment
Share on other sites

to be perfect yopu shoud try to create the file as he did.

O.K. that's not easy, as I would need the rest of his script...

I have no idea why Jon write such warning in the doc about handle/filename not to be mixed, but he DID.

Could you ask him. Maybe the warning needs to be more precise.

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Thank you very much for all your replies gentlemen

Indeed "out of the box" it works because I've built a test file and used the same code (without generating the file first but working directly on the file) and both methods worked fine. I guess there might be something in the script that -somehow - prevents my original way to be executed.

The issue was reading the file into an array - the content of the file could be any (in my case the content will always be the same because is a switch answer to a show snmp community command)

I would gladly post my script here but ... it is like 3000 lines long, it heavily use Tera Term and it needs an Extreme Network switch to work on :rolleyes: ... hard to get all of these.

The problem was solved by replacing the file handle with the filename.

Why this happened I don't know but I can be like 99% sure that it is my fault and NOT AutoIt. I started this post because I've been working for a couple of hours on this tiny piece of code and it failed to work and I was confused and angry and motivated to find why ... hehe ... nice collection of feelings :rambo:

As it happens often ... 5 minutes after starting the post I've found a solution (not an explanation tough :x

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

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...