Jump to content

Get all data untill it reads a colon per line.


Recommended Posts

Currently I have

$open = fileopen(@ScriptDir&"\test.txt", 0)
$read = fileread($open)
$text = $read

msgBox(0, "", StringBetween($text, "", ":"))

Func StringBetween($sString, $sStart, $sEnd)
    Local $sReturn
    $sReturn = _StringBetween($sString, $sStart, $sEnd)
    If @error Then Return ''
    Return $sReturn[0]
EndFunc

But this only read 1 line of the file, I need it to read all lines.

I only want to select the IP addresses

The file :

125.199.15.243:12324:username:password
275.111.175.243:12324:username:password
785.122.125.243:12324:username:password
385.166.115.243:12324:username:password
485.144.165.243:12324:username:password

hope someone can help me out.

Edited by xtreempje
Link to comment
Share on other sites

  • Developers

an example input test.txt and what you would expect retuned by the script would help to help you. :) 

Jos

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

Try this :

#include <Array.au3>
#include <Constants.au3>

Local $sFile = _
"125.199.15.243:12324:username:password" & @CRLF & _
"275.111.175.243:12324:username:password" & @CRLF & _
"785.122.125.243:12324:username:password" & @CRLF & _
"385.166.115.243:12324:username:password" & @CRLF & _
"485.144.165.243:12324:username:password"

Local $aResult = StringRegExp($sFile, ":(.*?):", $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($aResult)

 

Link to comment
Share on other sites

  • Developers

Like this?

#include <Array.au3>
#include <Constants.au3>

Local $sFile = _
"125.199.15.243:12324:username:password" & @CRLF & _
"275.111.175.243:12324:username:password" & @CRLF & _
"785.122.125.243:12324:username:password" & @CRLF & _
"385.166.115.243:12324:username:password" & @CRLF & _
"485.144.165.243:12324:username:password"

Local $aResult = StringRegExp($sFile, "(.*?):.*:", $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($aResult)

or when it the start of each line until ":":

Local $aResult = StringRegExp($sFile, "(?m)^(.*?):", $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($aResult)

 

Edited by Jos

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

Here is my try to this, a bit longer than with regexp but ...

 

#include <Array.au3>
#include <Constants.au3>
#include <String.au3>

Local $sFile = _
"125.199.15.243:12324:username:password" & @CRLF & _
"275.111.175.243:12324:username:password" & @CRLF & _
"785.122.125.243:12324:username:password" & @CRLF & _
"385.166.115.243:12324:username:password" & @CRLF & _
"485.144.165.243:12324:username:password"

Local $aSplit=_StringExplode($sFile,@crlf)
;_ArrayDisplay($aSplit)

For $x=0 to UBound($aSplit)-1
    ConsoleWrite ("Searching nr:" & $x & " : " & $aSplit[$x] & @crlf)
    $ip=_StringSearchSplit($aSplit[$x],":","L","L")
    CW($ip)
    $tmp=_StringSearchSplit($aSplit[$x],":","L","R")
    $port=_StringSearchSplit($tmp,":","L","L")
    CW($port)
    $tmp1=_StringSearchSplit(StringMid($tmp,StringInStr($tmp,":",0,1)),":","L","R")
    $Username=_StringSearchSplit($tmp1,":","L","L")
    $Password=_StringSearchSplit($tmp1,":","L","R")
    Cw($Username)
    CW($Password)
Next

Func CW($txt)
    ConsoleWrite ($txt & @CRLF)
EndFunc


Func _StringSearchSplit($str, $delimiter, $dir = "L", $ret = "R", $incdel = -1)
    ; #FUNCTION# ====================================================================================================================
    ; Name ..........:  _StringSearchSplit
    ; Description ...:  Search for the first delimiter in a string, with searching direction, Case Sensitive search
    ; Syntax ........:  _StringSearchSplit( $String, $delimiter [, $dir ] [, $ret ] [, $incdel ])
    ; Parameters ....:  $String             - String to be checked.
    ;                   $delimiter          - 1 char delimiter, has to be defined
    ;                   $dir                - Search from direction (Left/Right), use "L" or "R" - Left is default
    ;                                           The first letter will be used for direction, if multiple letters are entered e.g "Lab" = "L"
    ;                   $ret                - Return side, Left or Right - Right is default. see above for valid entries.
    ;                   $incdel             - Include delimiter 0 = No, 1 = Yes
    ;
    ; Return values .:  Success             - String
    ;
    ;                       e.g. 1: _StringSearch("c:\bat\test.bb","\","L","L")   returns "c:"
    ;                       e.g. 2: _StringSearch("c:\bat\test.bb","\","L","L",1) returns "c:\"
    ;                       e.g. 3: _StringSearch("c:\bat\test.bb","\","L","R")   returns "bat\test.bb"
    ;                       e.g. 4: _StringSearch("c:\bat\test.bb","\","L","R",1) returns "\bat\test.bb"
    ;                       e.g. 5: _StringSearch("c:\bat\test.bb","\","R","R")   returns "test.bb"
    ;                       e.g. 6: _StringSearch("c:\bat\test.bb","\","R","R",1) returns "\test.bb"
    ;
    ;                   Failure             - Empty string and @error flag as follows:
    ;                   @error :            1 - Empty String
    ;                                       2 - Delimiter should have a length of 1 char
    ;                                       3 - Should not happen, but if it does, search the PANIC button !
    ; Author ........:  Dan_555 (Autoitscript.com forum)
    ; ===============================================================================================================================

    Local $y
    Local $tmptxt = ""

    $dir = StringLeft(StringUpper($dir),1)
    $ret = StringLeft(StringUpper($ret),1)

    SetError(0)

    If StringLen($str) = 0 Then
        SetError(1)             ;empty string
        Return ""
    EndIf

    If (StringInStr($str, $delimiter) = 0) Or (StringLen($delimiter) <> 1) Then
        SetError(2)                ;invalid delimiter
        Return ""
    EndIf

    If $dir <> "L" And $dir <> "R" Then $dir = "L"                  ;Set default values
    If $ret <> "L" And $ret <> "R" Then $ret = "R"

    If $dir = "L" Then $y = StringInStr($str, $delimiter, 1)        ;Search for the delimiter
    If $dir = "R" Then $y = StringInStr($str, $delimiter, 1, -1)

    If $incdel = 0 Then $incdel = -1                                ;Tricky calculations ;)
    If $incdel <> -1 Then $incdel = 0

    If $ret = "L" Then Return StringMid($str, 1, $y + $incdel)      ;DisAssemble the string
    If $ret = "R" Then Return StringMid($str, $y - $incdel)

    SetError (3)
    Return ""
EndFunc   ;==>_StringSearchSplit

 

Some of my script sourcecode

Link to comment
Share on other sites

  • Developers
33 minutes ago, xtreempje said:

now I gotta find out how to save the array to a txt file.

One line should do it:

#include <File.au3>
_FileWriteFromArray("Testout.txt",$aResult)
;-or-
FileWrite("Testout.txt",_ArrayToString($aResult,@crlf))

 

Edited by Jos

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

I got 1 more question.

I have made an GUI and in the GUI I open another .au3 script with a gui and when I click save in that new opened gui it saves the .ini file in the folder were the first .au3 script is located instead of where the second one is located /extra ( folder ).

this is how it saves the ini

Local $sFile = GUICtrlRead($Edit1)
Local $hFile = FileOpen($sFile, $FO_OVERWRITE)
Local $aResult = StringRegExp($sFile, "(.*?):.*:", $STR_REGEXPARRAYGLOBALMATCH)
Filedelete(@ScriptDir & "Testout.ini")
Filewrite(@ScriptDir & "Testout.ini", "[Config]" & @crlf)
Filewrite(@ScriptDir & "Testout.ini", "edit2=")
FileWrite(@ScriptDir & "Testout.ini",_ArrayToString($aResult, "@R@", @crlf))

but now it names it : ExtraTestout instead of Testout.

Probl got the ScriptDir wrong I think?

Link to comment
Share on other sites

  • Developers

Guess your script directory ends with "\extra"...   that should be a good hint. :) 

EDIT: And what should this part do in your code: , "@R@", @crlf))  ?

Edited by Jos

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

  • Developers

That's the way learning works! ;) 

Have you seen my EDIT? Think that third parameter shouldn't be there as you replaced it with the second parameter value.

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

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