Jump to content

FileReadLine all lines...


Recommended Posts

;Direcionar ao arquivo de texto com informações das estações conforme selecionado no menu anterior.(FILXXX)

;Assign the file path to a variable
Local $sFilePath = @ScriptDir & "\Dados\FIL" & $sFilial & ".txt"

;Open the file temp.txt in append mode. If the folder C:\AutomationDevelopers does not exist, it will be created.
Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)

;Display a message box in case of any errors.
If $hFileOpen = -1 Then
   MsgBox($MB_SYSTEMMODAL, "", "Um erro ocorreu ao abrir o arquivo FIL" & $sFilial &".")
EndIf

;Set the file position to beginning for reading the data from the beginning of the file.
FileSetPos($hFileOpen, 0, $FILE_BEGIN)

;ler linha 2 do arquivo de texto (PRIMEIRA É COMENTÁRIO)
Local $sFileRead = FileReadLine ($hFileOpen,2)

;========================================================================================================================
;ESCOLHER ESTAÇÃO

;quebrar linha 2 do arquivo de texto em cada traço
$EST = StringSplit($sFileRead,"-")

;criar objeto lista em branco
$sList = $EST[1]

; Create a GUI
$hGUI = GUICreate("QUAL ESTAÇÃO ?", 350, 50)
; Create the combo
$hCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
; And fill it
GUICtrlSetData($hCombo, $sList)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hCombo
            $sEstacao = GUICtrlRead($hCombo)
            ExitLoop
    EndSwitch
 WEnd

My code look like this...Now I'm stuck in doing the FileReadLine read all the file lines, and then I'll try to separate the first line part from the second. 

EX: red station - 192.168.1.10

      yellow station - 192.168.1.11

the text file will have a line to each station I need to repair, I only need at first to show the stations names...And the IP's will be stored to be redirected after the selection...how can I select just the first part of each non blank line to make a list?...

Thanks again...and sorry if I didn't search correctly. 

Link to comment
Share on other sites

You have to make a loop to read the file line by line.

You can also use FileReadToArray to build an array from your file, each line in a a row. Here is an example :

#Include <Array.au3>
#Include <GUIConstantsEx.au3>

Local $sFilePath = @ScriptDir & "\Dados\FIL" & $sFilial & ".txt"

Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)
Local $aStations[1][2], $i = 0

Local $aLines = FileReadToArray($hFileOpen)
Local $aStations[UBound($aLines)][2]
;~ _ArrayDisplay($aStations)
Local $iCount = 0, $sList

For $i = 0 To UBound($aLines) - 1
   If Not StringLen($aLines[$i]) Then ContinueLoop
      $iCount += 1
      $aStations[$iCount - 1][0] = StringRegExpReplace($aLines[$i], "\h*-.+", "")
      $aStations[$iCount - 1][1] = StringRegExpReplace($aLines[$i], ".+?-\h*", "")
      $sList &= $aStations[$iCount - 1][0] & "|"
Next
Redim $aStations[$iCount][2]

Local $hGUI = GUICreate("GUI", 350, 50)
Local $hCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($hCombo, $sList)
GUISetState()
While 1
   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         Exit
   EndSwitch
 WEnd

 

Link to comment
Share on other sites

You could also use _FileReadToArray()

#include <Array.au3>
#include <GuiComboBox.au3>
#include <File.au3>
#include <GUIConstants.au3>
Local $sFilial = "Stations"
Local $sFilePath = @ScriptDir & "\Dados\FIL" & $sFilial & ".txt"
Local $aStations
    _FileReadToArray($sFilePath, $aStations, 4, " - ")

GUICreate("QUAL ESTAÇÃO ?", 350, 50)
Local $idCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($idCombo, _ArrayToString($aStations, "|", -1, -1, "|", 0, 0))
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idCombo
            $iCombo = _GUICtrlComboBox_GetCurSel($idCombo)
            MsgBox(4096, "Selection", "Station : " & $aStations[$iCombo][0] & @CRLF & "IP Address : " & $aStations[$iCombo][1])
    EndSwitch
 WEnd

 

Link to comment
Share on other sites

14 hours ago, edumanilha said:

EX: red station - 192.168.1.10

      yellow station - 192.168.1.11

With such a text format an ini file would be a much easier way ...

#Include <Array.au3>

$newtext = "[stations]" & @crlf & StringReplace(FileRead("test.txt"), " - ", "=")
FileWrite("test.ini", $newtext)
$res = IniReadSection("test.ini", "stations")
_ArrayDisplay($res)

 

Edited by mikell
typo
Link to comment
Share on other sites

18 hours ago, jguinch said:

You have to make a loop to read the file line by line.

You can also use FileReadToArray to build an array from your file, each line in a a row. Here is an example :

#Include <Array.au3>
#Include <GUIConstantsEx.au3>

Local $sFilePath = @ScriptDir & "\Dados\FIL" & $sFilial & ".txt"

Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)
Local $aStations[1][2], $i = 0

Local $aLines = FileReadToArray($hFileOpen)
Local $aStations[UBound($aLines)][2]
;~ _ArrayDisplay($aStations)
Local $iCount = 0, $sList

For $i = 0 To UBound($aLines) - 1
   If Not StringLen($aLines[$i]) Then ContinueLoop
      $iCount += 1
      $aStations[$iCount - 1][0] = StringRegExpReplace($aLines[$i], "\h*-.+", "")
      $aStations[$iCount - 1][1] = StringRegExpReplace($aLines[$i], ".+?-\h*", "")
      $sList &= $aStations[$iCount - 1][0] & "|"
Next
Redim $aStations[$iCount][2]

Local $hGUI = GUICreate("GUI", 350, 50)
Local $hCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($hCombo, $sList)
GUISetState()
While 1
   Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
         Exit
   EndSwitch
 WEnd

 

When I try this, I got this error:

2020-01-31 14.22.33.jpg

Link to comment
Share on other sites

16 hours ago, Subz said:

You could also use _FileReadToArray()

#include <Array.au3>
#include <GuiComboBox.au3>
#include <File.au3>
#include <GUIConstants.au3>
Local $sFilial = "Stations"
Local $sFilePath = @ScriptDir & "\Dados\FIL" & $sFilial & ".txt"
Local $aStations
    _FileReadToArray($sFilePath, $aStations, 4, " - ")

GUICreate("QUAL ESTAÇÃO ?", 350, 50)
Local $idCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($idCombo, _ArrayToString($aStations, "|", -1, -1, "|", 0, 0))
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idCombo
            $iCombo = _GUICtrlComboBox_GetCurSel($idCombo)
            MsgBox(4096, "Selection", "Station : " & $aStations[$iCombo][0] & @CRLF & "IP Address : " & $aStations[$iCombo][1])
    EndSwitch
 WEnd

 

When I try this I got this error...

2020-01-31 14.29.43.jpg

Link to comment
Share on other sites

  • Developers

Could you please simply post the text error instead of these pictures? 
Just run your script through au3check, which will give you a much better error check of the code.

Thanks

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Also recommend downloading and installing the full AutoIt Scite Editor if you haven't already done so, as this will help with debugging?

Just a guess but when you copied the code above it may have included a hidden character which caused the parsing error, it happens to me occasionally on this forum.  Usually running the code in Scite will show you which line has the hidden character and so you can delete it or use something like Notepad++ to show all characters.

With @jguinch code, change:

Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)

To

Local $hFileOpen = FileOpen($sFilePath)

While $FO_APPEND + $FO_CREATEPATH would normally require #include <File.au3> at the top of your script, you can't read a file that has been opened in write mode.

Link to comment
Share on other sites

4 hours ago, Jos said:

Could you please simply post the text error instead of these pictures? 
Just run your script through au3check, which will give you a much better error check of the code.

Thanks

Wow! On the phone the pics dont seen so huge...Really sorry!

Link to comment
Share on other sites

2 hours ago, Subz said:

Also recommend downloading and installing the full AutoIt Scite Editor if you haven't already done so, as this will help with debugging?

Just a guess but when you copied the code above it may have included a hidden character which caused the parsing error, it happens to me occasionally on this forum.  Usually running the code in Scite will show you which line has the hidden character and so you can delete it or use something like Notepad++ to show all characters.

With @jguinch code, change:

Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)

To

Local $hFileOpen = FileOpen($sFilePath)

While $FO_APPEND + $FO_CREATEPATH would normally require #include <File.au3> at the top of your script, you can't read a file that has been opened in write mode.

Thanks for the hints! I'll try both...

Link to comment
Share on other sites

3 hours ago, Subz said:

Also recommend downloading and installing the full AutoIt Scite Editor if you haven't already done so, as this will help with debugging?

Just a guess but when you copied the code above it may have included a hidden character which caused the parsing error, it happens to me occasionally on this forum.  Usually running the code in Scite will show you which line has the hidden character and so you can delete it or use something like Notepad++ to show all characters.

With @jguinch code, change:

Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)

To

Local $hFileOpen = FileOpen($sFilePath)

While $FO_APPEND + $FO_CREATEPATH would normally require #include <File.au3> at the top of your script, you can't read a file that has been opened in write mode.

No progress...

Link to comment
Share on other sites

On 1/30/2020 at 9:49 PM, Subz said:

You could also use _FileReadToArray()

#include <Array.au3>
#include <GuiComboBox.au3>
#include <File.au3>
#include <GUIConstants.au3>
Local $sFilial = "Stations"
Local $sFilePath = @ScriptDir & "\Dados\FIL" & $sFilial & ".txt"
Local $aStations
    _FileReadToArray($sFilePath, $aStations, 4, " - ")

GUICreate("QUAL ESTAÇÃO ?", 350, 50)
Local $idCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($idCombo, _ArrayToString($aStations, "|", -1, -1, "|", 0, 0))
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idCombo
            $iCombo = _GUICtrlComboBox_GetCurSel($idCombo)
            MsgBox(4096, "Selection", "Station : " & $aStations[$iCombo][0] & @CRLF & "IP Address : " & $aStations[$iCombo][1])
    EndSwitch
 WEnd

 

I just deleted $sFilial, because I already have this value from previous selection...

I tried this part:

Local $aStations
    _FileReadToArray($sFilePath, $aStations, 4, " - ")

The way it looks above and:
Local $aStations = _FileReadToArray.....

Link to comment
Share on other sites

On 1/31/2020 at 8:17 PM, Subz said:

Well as I mentioned I've tested both @jguinch and my own code and it works fine, so it's something on your side, you need to debug from your side and figure out what's wrong.

Managed to do something like this:

 

;========================================================================================================================
;Direcionar ao arquivo de texto com informações das estações conforme selecionado no menu anterior.(FILXXX)

;Assign the file path to a variable
Local $sFilePath = @ScriptDir & "\Dados\FIL" & $sFilial & ".txt"

;Open the file temp.txt in append mode. If the folder C:\AutomationDevelopers does not exist, it will be created.
Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)

;Display a message box in case of any errors.
If $hFileOpen = -1 Then
   MsgBox($MB_SYSTEMMODAL, "", "Um erro ocorreu ao abrir o arquivo FIL" & $sFilial &".")
EndIf

;Set the file position to beginning for reading the data from the beginning of the file.
FileSetPos($hFileOpen, 0, $FILE_BEGIN)

;ler linha 2 do arquivo de texto (PRIMEIRA É COMENTÁRIO)
Local $sFileRead = FileReadLine ($hFileOpen,2)

;========================================================================================================================
;ESCOLHER ESTAÇÃO

;quebrar linha 2 do arquivo de texto em cada virgula
$ESTINFO = StringSplit($sFileRead,",")
;criar objeto lista em branco
$sList = ""

;loop de 1 até último item, criando opções da lista
For $i = 1 To UBound($ESTINFO) - 1
    $sList &= "|" & $ESTINFO[$i]
Next

;loop de 1 até último item, montando lista e mostrando só o nome!
;For $i = 1 To UBound($ESTINFO) - 1
;   $EST = StringSplit($ESTINFO[$i],":")
;   $sList &= "|" & $EST[1]
;Next

MsgBox($MB_SYSTEMMODAL, "", $ESTINFO[1] & $ESTINFO[2])

; Create a GUI
$hGUI = GUICreate("QUAL ESTAÇÃO DA FIL" & $sFilial & " ?", 350, 50)
; Create the combo
$hCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
; And fill it
GUICtrlSetData($hCombo, $sList)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hCombo
            $sEstacao = GUICtrlRead($hCombo)
            ExitLoop
    EndSwitch
 WEnd

I don't know why the others codes didn't work...I tried NPP++ and the scyte you mention...Now My list appears, but the same way it's on the file..."Name-IP". How Can I erase the -IP part? I tried StringReplace, RegexReplace,etc...So I can show only the name to the user and make a split on the chosen option to direct me to the correct station!

The menu look like this: Red Station-192.168.1.6

 

Thanks!

 

image.png

Link to comment
Share on other sites

12 hours ago, Subz said:

Both posts above already explain this, I've attached the example I used.FILStations.au3FILStations.txt

You script really works...I dont know whar I'm doing wrong...On the upper part, I commented everything but $sFilePath...The list only show -1...

My txt file have a header...

First line : instructions
second and third line: stations names and IP's..

But I tried remove the reader and all...

 

;Assign the file path to a variable
Local $sFilePath = @ScriptDir & "\Dados\FIL" & $sFilial & ".txt"

;Open the file temp.txt in append mode. If the folder C:\AutomationDevelopers does not exist, it will be created.
;Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $FO_CREATEPATH)

;Display a message box in case of any errors.
;If $hFileOpen = -1 Then
;   MsgBox($MB_SYSTEMMODAL, "", "Um erro ocorreu ao abrir o arquivo FIL" & $sFilial &".")
;EndIf

;Set the file position to beginning for reading the data from the beginning of the file.
;FileSetPos($hFileOpen, 0, $FILE_BEGIN)

;ler linha 2 do arquivo de texto (PRIMEIRA É COMENTÁRIO)
;Local $sFileRead = FileReadLine ($hFileOpen,2)







Local $aStations
    _FileReadToArray($sFilePath, $aStations, 4, " - ")

GUICreate("QUAL ESTAÇÃO ?", 350, 50)
Local $idCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($idCombo, _ArrayToString($aStations, "|", -1, -1, "|", 0, 0))
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idCombo
            $iCombo = _GUICtrlComboBox_GetCurSel($idCombo)
            MsgBox(4096, "Selection", "Station : " & $aStations[$iCombo][0] & @CRLF & "IP Address : " & $aStations[$iCombo][1])
    EndSwitch
 WEnd

 

Link to comment
Share on other sites

On 1/31/2020 at 6:26 PM, edumanilha said:

I didnt understood what you did...

Sorry. My bad, I didn't provide explanations
What my code does is : from a "test.txt" file containing this

red station - 192.168.1.10
yellow station - 192.168.1.11

It first builds a "test.ini" file which content is

[stations]
red station=192.168.1.10
yellow station=192.168.1.11

Then using the IniReadSection function you can directly get a 2D array to use in the script

#Include <Array.au3>
#include <GUIConstants.au3>

; create the ini (for the example)
$newtext = "[stations]" & @crlf & StringReplace(FileRead("test.txt"), " - ", "=")
FileWrite("test.ini", $newtext)
; read to a 2D array
$res = IniReadSection("test.ini", "stations")
; _ArrayDisplay($res)  ; display

GUICreate("QUAL ESTAÇÃO ?", 350, 50)
Local $idCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($idCombo, _ArrayToString($res, "|", 1, -1, "|", 0, 0))
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idCombo
        For $i = 1 to $res[0][0]
            If GuiCtrlRead($idCombo) = $res[$i][0] Then Msgbox(0,"", $res[$i][1])
        Next
    EndSwitch
Wend

 

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