Sign in to follow this  
Followers 0
mentosan

_FileReadToArray

8 posts in this topic

I'm using _FileReadToArray ($file, $numbers) to read numbers within a file, but I want only the first 8 numbers (digits) to be read. Which function shall i use for this purpose ?

Thx

Share this post


Link to post
Share on other sites



I'm using _FileReadToArray ($file, $numbers) to read numbers within a file, but I want only the first 8 numbers (digits) to be read. Which function shall i use for this purpose ?

One digit per line, read just the first 8 lines?

Or the first 8 Chars from the 1st line?

Or the first occurence of "8 Numbers" in that file?

Pls post an example for the file you want to read from (some part of it) and point out, WHAT you want to read from it.

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

The file contains only one line, with an IP address and a port number. So for example the content of file is:

192.123.10.1 4001

I only need the 192.123.10.1

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi, many ways you can do what your after, here's one way

$sFile = @ScriptDir & "\file1.txt"
$FR = FileRead($sFile)
ConsoleWrite(StringLeft($FR, StringInStr($FR, " ", 0, -1) - 1) & @LF)

file1.txt contained

192.123.10.1 4001

Cheers

Edited by smashly

Share this post


Link to post
Share on other sites

$s_text = "192.123.10.1 4001" & @CRLF & "139.321.20.3 4002"
$s_ip = StringRegExpReplace($s_text, "(?:.*?)(\d+\.\d+\.\d+\.\d+)(\s*\d+)", "\1")
ConsoleWrite($s_ip & @CRLF)
As long as the setup is the same with the port, this will get as many IPs that are in the file.


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.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

It is not working. Look, this is my code. I have tried to adapt what you have suggested :

#include <file.au3>

Dim $ipfile="host.txt"
Dim $ipaddress

_FileReadToArray ($ipfile, $ipaddress)

$file = FileRead ("host.txt")
$result = (StringLeft($file, StringInStr($file, " ", 0, -1) - 1) & @LF)
;$result = StringRegExpReplace($file, "(?:.*?)(\d+\.\d+\.\d+\.\d+)(\s*\d+)", "\1")

If Ping ($result) Then

Run ("telnet -f log_telnet.txt")

WinActivate("system32")
WinWaitActive("system32")

Send ("open " & $ipaddress[1] & "{Enter}")
WinWaitActive("Telnet " & $ipaddress[1])

 While 1
ControlSend ("Telnet","","","x" & "{Enter}")
Wend

Else 
    MsgBox(0,"Info", "No ping", 3)
    Exit

EndIf

Just to clarify myself: this code should first make a ping to the host ,and if the ping is OK, then it will run the command in the loop (While 1...Wend). Because the host is written in a txt file in this form: <IP address><space><port number>, ping will not work. You can only ping IP address, without port number. That's why I need only IP address string, with no alteration of the rest of the file which will be used later in the script.

Edited by mentosan

Share this post


Link to post
Share on other sites

Hi.

The file contains only one line, with an IP address and a port number. So for example the content of file is:

192.123.10.1 4001

I only need the 192.123.10.1

So the most simple is to split the string at blanks and to take the fist element:

#include <array.au3> ; that's for _ArrayDisplay()

$handle=fileopen("C:\IPandPORT.txt",0)
$MyLine=filereadline($handle) ; read one line = the 1st line
fileclose($handle)
$SplitArr=StringSplit($MyLine," ") ; split string at each blank, here split into IP and Port
_ArrayDisplay($SplitArr) ; just to display ;)
$IP=$SplitArr[1]
MsgBox(0,"IP Address",$IP)

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

It is OK now! That's what I was looking for. Great function to split strings! Thanks Rudi !

Cheers !

Edited by mentosan

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