Jump to content
Sign in to follow this  
juicyjon

Filereadline/filecountline help

Recommended Posts

juicyjon

I am having trouble getting autoit to read the lines of a file and then out put it to a message box. The message box works fine but the filelineread does not ;) , it tell me that there is only one line in file1 when i know there is over 100. Any one got any ideas on how to get it to work.

Thanks

$i = 1
$h = 1

Do
   ;MsgBox (0," H equals",$h)
   $line1 = FileReadLine($file1, $i)
   ;MsgBox (0,"lineread",$line)
   $a = 1
   $line = FileReadLine($file, $h)
   ;MsgBox (0,"line",$line1)
   $res = StringCompare ($line, $line1)
   ;MsgBox (0,"line1",$line1)
   ;MsgBox (0,"line",$line)
   ;Msgbox(0, "Compare", $res)
   $search = _ArraySearch ($array1, $line)
   Msgbox(0, "array search result", $search)
                              
   If $search > 0 Then
   $a = 2
   EndIf
   ;Msgbox(0, "A", $a)
   If $a = 1 Then
   $string = $string & $line & @CRLF
   EndIf
   $h = $h + 1
   ;MsgBox (0," I equals",$i)
   $i = $i + 1
              
until $i = $x+1
   ;Msgbox(0 , "Results", $string)

The Juiciest Of The Jons

Share this post


Link to post
Share on other sites
hannes08

Hi juicyjon,

take a Look at the example of FileReadLine() in the help file.

It loops until the error message is -1

I cannot see from you example when you stop reading lines.

If $x is not defined it will stop after the first read line.

IMHO it would be easier to read both files into array and then compare the arrays.

Look at the _FileReadToArray() function in the helpfile.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites
water

$i = 1
$h = 1
$line1 = FileReadLine($file1, $i)
$line = FileReadLine($file, $h)

Both FileReadLine statements read the same line (line number 1). Is this what you intend to do?

There is a function called _FileCountLines that returns the number of files in a file.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Mechaflash

Not sure what you're ultimately trying to accomplish...

If you provide your full code and more explanation, someone may be able to provide with a better/easier way of doing what you want to accomplish.

Edited by mechaflash213

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
juicyjon

I am trying to compare two files of IP addresses for my firewall. I first do a scan with a batch file and then i am trying to use autoit to compare the IPlist and the results file and then create a results1 file with the updated IP list. The lines about nmap are void because i found that nmap would not work with how it supplies its results. So basically i am trying to compare two files of IP addresses, update the list with the new results ( the IPs that are down) and then place those results in a file called results1.txt.

#include <IE.au3>
#include <String.au3>
#include <Array.au3>
#Include <File.au3>
DIM $string
;***************************************************************************************************************
;Opens fwiplist.txt, results.txt and results1.txt, but results1.txt is in overwrite.
$file = FileOpen ("C:\fwiplist.txt",0)
$file1 = FileOpen ("C:\results.txt",0)
$file2 = FileOpen ("C:\results1.txt",2)
$filey = FileRead ($file1)
;***************************************************************************************************************

;MsgBox (0,"line",$filey)
;$filex = Fileread ($file)
;MsgBox (0,"line",$filex)
;***************************************************************************************************************
;counts the number of lines in fwiplist.txt
$x = _FileCountLines ("C:\fwiplist.txt")
;***************************************************************************************************************
;MsgBox (0,"line",$x)
;$y = _FileCountLines ("C:\Documents and Settings\brea\My Documents\FW-Check-Script\results.txt")
;MsgBox (0,"line",$y)
;$filey = StringReplace ($filey, "Nmap scan report for ", "")
;$filey = StringReplace ($filey, "Host is up (", "")
;$filey = StringReplace ($filey, "# Nmap 5.51 scan initiated", "")
;$filey = StringReplace ($filey, "# Nmap done at", "")
;$filey = StringReplace ($filey, "IP addresses (", "")
;$filey = StringReplace ($filey, "2012 as: nmap -iL E:\documents\fwiplist1.txt -sn -oN E:\documents\results.txt", "")
;$filey = StringReplace ($filey, "hosts up) scanned in", "")
;$filey = StringReplace ($filey, "seconds", "")
;$filey = StringReplace ($filey, "s latency).", "")

$array1 = StringSplit($filey, @CRLF)
$i = 1
$h = 1
;***************************************************************************************************************
; Do loop which will read every line of results.txt and then compare it to the fwiplist.txt, then it will search the array for
;the line it read i.e. 8.8.8.8, if there it will then write it to results1.txt untill the end of results.txt.
;***************************************************************************************************************
Do
   ;MsgBox (0," H equals",$h)
   $line1 = FileReadLine($file1, $i)
   ;MsgBox (0,"lineread",$line)
   $a = 1
   $line = FileReadLine($file, $h)
   ;MsgBox (0,"line",$line1)
   $res = StringCompare ($line, $line1)
   ;MsgBox (0,"line1",$line1)
   ;MsgBox (0,"line",$line)
   ;Msgbox(0, "Compare", $res)
   $search = _ArraySearch ($array1, $line)
   ;Msgbox(0, "array search result", $search)
                               
   If $search > 0 Then
   $a = 2
   EndIf
   ;Msgbox(0, "A", $a)
   If $a = 1 Then
   $string = $string & $line & @CRLF
   EndIf
   $h = $h + 1
   ;MsgBox (0," I equals",$i)
   $i = $i + 1
               
until $i = $x+1
   ;Msgbox(0 , "Results", $string)
;***************************************************************************************************************
FileWrite($file2, $string)
;***************************************************************************************************************
;Closes all files that it opened.
FileClose($file)
FileClose($file1)
FileClose($file2)
;***************************************************************************************************************
;copies results.txt to fwiplist.txt.
FileCopy("C:\results.txt", "C:\fwiplist.txt", 1)
;***************************************************************************************************************
; Only include this line once you know it gives you the results you want as it will copy results1.txt over your existing fwiplist1.txt
; I tested it using other filenames with just a few IP's in the filenames and it resulted in a results1.txt file without the IP's listed
; in results.txt
;
;FileCopy("E:\documents\results1.txt", "E:\documents\fwiplist1.txt", 1)
;***************************************************************************************************************

The Juiciest Of The Jons

Share this post


Link to post
Share on other sites
Mechaflash

Not tested

Global $asOld = _FileReadToArray($OldipList)
Global $asNew = _FileReadToArray($NewipList)
Global $asDiff[999], $x = 0
Global $delimiter = ","

For $i = 1 to $asNew[0] Step 1
  If _ArraySearch($asOld, $asNew[$i]) < 0 Then
    $asDiff[$x] = $asNew[$i]
    $x+=1
  EndIf
Next

ReDim $asDiff[$x]
FileWrite($results, "Below is a comma delimited list of new IP addresses:" & @LF & _ArrayToString($asDiff, $delimiter))
Edited by mechaflash213

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
juicyjon

That bit of code you replied with will replace my do loop correct. Just wan to make sure i put it in the right place.

Thanks

Edited by juicyjon

The Juiciest Of The Jons

Share this post


Link to post
Share on other sites
Mechaflash

that'll take your old IP list and your new one and compare them, then whatever new IPs are found, it will throw into your results. You can reverse the array lookup in the for loop and your results will be what IP addresses are missing.


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
juicyjon

OK, thanks! It works now. Thanks for all the quick replies and answers. ;)


The Juiciest Of The Jons

Share this post


Link to post
Share on other sites
Mechaflash

I made a mistake on the ReDim. Edited it from Redim $asDiff[ubound($asDiff)] to Redim $asDiff[$x]


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
juicyjon

Both FileReadLine statements read the same line (line number 1). Is this what you intend to do?

There is a function called _FileCountLines that returns the number of files in a file.

actually they read two different lines Water. The first fiereadline reads the first line of file1 and the other filereadline reads the first line of file. (not file 1).

The Juiciest Of The Jons

Share this post


Link to post
Share on other sites
water

Oops, sorry. I missed that detail ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
juicyjon

its all good Water, thanks for the input. ;)


The Juiciest Of The Jons

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  

×