I have test.csv file need to read all the data using autoit
As TeamKiller said your question is quite vague but here is a sample code that should give you an idea of how to read a CSV file.
#include <GUIConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <String.au3>
Opt("MustDeclareVars", 1)
Global Const $CSVFILE = "C:\Temp\test.csv"
Global Const $DELIM = ";" ;the delimiter in the CSV file
Global $i, $arrContent, $arrLine, $res = 0
$res = _FileReadToArray($CSVFILE, $arrContent)
If $res = 1 Then
For $i = 1 To $arrContent[0]
$arrLine = StringSplit($arrContent[$i], $DELIM)
If IsArray($arrLine) And $arrLine[0]<>0 Then
_ArrayDisplay($arrLine)
; do something with the elements of the line
Else
MsgBox(48, "", "Error splitting line!")
EndIf
Next
Else
MsgBox(48, "", "Error opening file!")
EndIf
return value of _ParseCSV() is 2D array like
$array[1][1] first line first data
$array[1][2] first line second data
$array[3][5] 3rd line 5th data
$array[0][0] gives number of lines
$array[0][1] gives number of data in each line
no need for includes
params:
filename
delimeter
message display if cannot open file
logical true/false to skip the first line of the file
;_ParseCSV("filename",",","message if error happens",true)
Func _ParseCSV($f,$Dchar,$error,$skip)
Local $array[500][500]
Local $line = ""
$i = 0
$file = FileOpen($f,0)
If $file = -1 Then
MsgBox(0, "Error", $error)
Return False
EndIf
;skip 1st line
If $skip Then $line = FileReadLine($file)
While 1
$i = $i + 1
Local $line = FileReadLine($file)
If #error = -1 Then ExitLoop
$row_array = StringSplit($line,$Dchar)
If $i == 1 Then $row_size = UBound($row_array)
If $row_size <> UBound($row_array) Then MsgBox(0, "Error", "Row: " & $i & " has different size ")
$row_size = UBound($row_array)
$array = _arrayAdd_2d($array,$i,$row_array,$row_size)
WEnd
FileClose($file)
$array[0][0] = $i-1 ;stores number of lines
$array[0][1] = $row_size -1 ; stores number of data in a row (data corresponding to index 0 is the number of data in a row actually that's way the -1)
Return $array
EndFunc
Func _arrayAdd_2d($array,$inwhich,$row_array,$row_size)
For $i=1 To $row_size -1 Step 1
$array[$inwhich][$i] = $row_array[$i]
Next
Return $array
EndFunc
As for parsing a CSV file, you are likely better off using a library (called user-defined functions in AutoIt), especially if you e.g. have complex CSVs with quoted strings (commas inside of the "cell"/string) or line breaks, which are hard to handle.
The best I can recommend is CSVSplit.
Basically you have a function _CSVSplit that takes a whole CSV file (content, i.e. string!) and returns you a two-dimensional array:
Local $sCSV = FileRead($sFilePath)
If #error Then ; ....
$aSplitArray = _CSVSplit($sCSV, ",")
You can then do everything you want with this array. Obviously, CSVSplit also provides the "reverse" function for turning an array into a CSV string again, _ArrayToCSV.
Originally posted as an answer here, which I consider a duplicate of this question.
Related
I want to ask you a question regarding Shell.Application COM Object. The programming language I work in is AutoIT. I would like to know, how can I open a folder in the exact same Window a previous folder has been opened.
For instance, if I open the C:\Folder in a Window and it has a subfolder, then how can I make the Script open that subfolder in the same window the "parent folder" has been opened, without creating a new window for that. The only way I can open a Folder is like this:
global $ShellApplication = ObjCreate("Shell.Application")
$ShellApplication.Explore("C:\Folder")
Sleep(100)
$ShellApplication.Explore("C:\Folder\Subfolder")
but this way is the wrong way. I hope someone has got any idea about how this could be achieved.
I don't know whether this is what you want, but you can automate the current window like this:
#include <Array.au3>
Opt("SendKeyDelay", 1) ;5 milliseconds
Opt("SendKeyDownDelay", 1) ;1 millisecond
$pid = ShellExecute("explorer.exe ", #DesktopDir)
Sleep(300)
;~ $aWinList=WinList("[REGEXPCLASS:(Explore|Cabinet)WClass]")
;~ _ArrayDisplay($aWinList)
ConsoleWrite($pid & #CRLF)
$handle = _WinGetHandleByPID($pid)
Sleep(2000)
SendKeepActive($handle)
Send('{F4}')
Send('^a')
Send(#AppDataCommonDir & "{ENTER}")
Func _WinGetHandleByPID($vProcess, $nShow = 1)
; Get Window Handle by PID
; Author Hubertus
; derived from Smoke_N's script with the same name,
; but to handle more than one process with the same name
; and to return handle of newest window ($nShow = 2).
;
; $vProcess = Processname(e.g. "Notepad.exe") or Processnumber(e.g. 4711 )
; $nShow = -1 "All (Visble or not)", $nShow = 0 "Not Visible Only", $nShow = 1 "Visible Only", $nShow = 2 "return handle of newest window "
; #error = 0 Returns array of matches. Array[0][0] and #extended shows number of matches.
; #error = 0 and $nShow = 2 return handle of newest window
; Array[n][0] shows windows title. Array[n][1] shows windows handle. n = 1 to #extended
; #error = 1 Process not found.
; #error = 2 Window not found.
If Not ProcessExists($vProcess) Then Return SetError(1, 0, 0) ; no matching process
Local $iWinList, $aWinList = WinList()
Local $iResult, $aResult[UBound($aWinList)][2]
Local $iProcessList, $aProcessList = ProcessList($vProcess)
If $aProcessList[0][0] = 0 Then Local $aProcessList[2][2] = [[1, 0], ["", $vProcess]]
For $iWinList = 1 To $aWinList[0][0]
For $iProcessList = 1 To $aProcessList[0][0]
If WinGetProcess($aWinList[$iWinList][1]) = $aProcessList[$iProcessList][1] Then
If $nShow > -1 And Not $nShow = (2 = BitAND(WinGetState($aWinList[$iWinList][1]), 2)) Then ContinueLoop
$iResult += 1
$aResult[$iResult][0] = $aWinList[$iWinList][0]
$aResult[$iResult][1] = $aWinList[$iWinList][1]
EndIf
Next
Next
If $iResult = 0 Then Return SetError(2, 0, 0) ; no window found
ReDim $aResult[$iResult + 1][2]
$aResult[0][0] = $iResult
If $nShow = 2 Then Return SetError(0, $iResult, $aResult[1][1])
Return SetError(0, $iResult, $aResult)
EndFunc ;==>_WinGetHandleByPID
At the moment I'm trying to decode a packet from a ntp server. I'm trying it in AutoIT, that's a simple script language.
I found some code but the problem is, I need the milliseconds too. The code only provides seconds.
Thats the code I found:
Local $ntpServer = 'pool.ntp.org'
UDPStartup()
Dim $socket = UDPOpen(TCPNameToIP($ntpServer), 123)
If #error <> 0 Then Return SetError(1)
; $status = UDPSend($socket, MakePacket('1b0e010000000000000000004c4f434ccb1eea7b866665cb00000000000000000000000000000000cb1eea7b866665cb'))
Local $status = UDPSend($socket, MakePacket('1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'))
If $status = 0 Then Return SetError(1)
Local $data = '', $a = TimerInit() ; Timer setzen, damit im Fehlerfall die Schleife abgebrochen wird
While $data = ''
$data = UDPRecv($socket, 100)
Sleep(100)
If TimerDiff($a) > 1000 Then ExitLoop ; Wenn Timer > 1sek. (Fehler), dann Schleife verlassen
WEnd
If $data <> '' Then
UDPShutdown()
Local $unsignedHexValue = StringMid($data, 83, 8) ; Extract time from packet. Disregards the fractional second.
Local $value = UnsignedHexToDec($unsignedHexValue)
Local $TZinfo = _Date_Time_GetTimeZoneInformation()
Local $TZoffset = -(UnsignedToLong($TZinfo[1]) + (UnsignedToLong($TZinfo[4])*($TZinfo[0]<2)) + (UnsignedToLong($TZinfo[7])*($TZinfo[0]=2)))
Local $UTC = _DateAdd('s', $value, '1900/01/01 00:00:00')
Return _DateAdd('n', $TZoffset, $UTC)
EndIf
SetError(1)
It works fine for seconds. As you see, there is a comment "Disregards the fractional second". So, I checked how a ntp packet works and it shows that you have 32bit for seconds, 32 bits for fractional seconds and then another 64 bits with secs and fractions sine 1.1.1900.
In the code he uses 8 chars from the packet (I have no clue why he starts at 83), convert that from hex to dec and then you have the time. So, my first thought to get the milliseconds was, get the next 8 chars, convert that and I have the milli secs, but it's not.
If I get the time four times and let the programm sleep between that, then print out the time with milliseconds I get stuff like that:
2019/02/14 14:29:56.987628606
2019/02/14 14:29:56.243...
2019/02/14 14:29:56.388...
2019/02/14 14:29:57.1107...
That can't be correct. So there is some mistake in decoding the time packet. Anyone know how? I don't need the time extrem exactly like on the millisecond, its enough in a range of 200 - 300 ms.
Edit:
Okay, as far as I tested out, the problem is at the converting of the fraction to millisecs.
For all the infos wanted in the comments, thats the complete code:
Func _TimeSync()
Local $ntpServer = 'pool.ntp.org'
UDPStartup()
Dim $socket = UDPOpen(TCPNameToIP($ntpServer), 123)
If #error <> 0 Then Return SetError(1)
;$status = UDPSend($socket, MakePacket('1b0e010000000000000000004c4f434ccb1eea7b866665cb00000000000000000000000000000000cb1eea7b866665cb'))
Local $status = UDPSend($socket, MakePacket('1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'))
If $status = 0 Then Return SetError(1)
Local $data = '', $a = TimerInit() ; Timer setzen, damit im Fehlerfall die Schleife abgebrochen wird
While $data = ''
$data = UDPRecv($socket, 100)
sleep(100)
If TimerDiff($a) > 1000 Then ExitLoop ; Wenn Timer > 1sek. (Fehler), dann Schleife verlassen
WEnd
If $data <> '' Then
UDPShutdown()
Local $unsignedHexValue = StringMid($data,83,8); Extract time from packet. Disregards the fractional second.
Local $unsignedHexValue2 = StringMid($data,91,8); Extract time from packet. Disregards the fractional second.
Local $value = UnsignedHexToDec($unsignedHexValue)
Local $value2 = UnsignedHexToDec($unsignedHexValue2)
Local $TZinfo = _Date_Time_GetTimeZoneInformation()
Local $TZoffset = -(UnsignedToLong($TZinfo[1]) + (UnsignedToLong($TZinfo[4])*($TZinfo[0]<2)) + (UnsignedToLong($TZinfo[7])*($TZinfo[0]=2)))
Local $UTC = _DateAdd('s',$value,'1900/01/01 00:00:00')
Return _DateAdd('n',$TZoffset,$UTC)&"."&($value2)
EndIf
SetError(1)
EndFunc
Func MakePacket($d)
Local $p = ''
While $d
$p &= Chr(Dec(StringLeft($d,2)))
$d = StringTrimLeft($d,2)
WEnd
Return $p
EndFunc
Func UnsignedHexToDec($n)
Local $ones = StringRight($n,1)
$n = StringTrimRight($n,1)
Return dec($n)*16+dec($ones)
EndFunc
Func UnsignedToLong($Value)
Local Const $OFFSET_4 = 4294967296
Local Const $MAXINT_4 = 2147483647
If $Value < 0 Or $Value >= $OFFSET_4 Then Return SetError(1,0,$Value) ;' Overflow
If $Value <= $MAXINT_4 Then
Return $Value
Else
Return $Value - $OFFSET_4
EndIf
EndFunc
I couldnt figured out how to convert the fractions to a normal int or float
The fractional seconds are measured in units of 1/(2**numberOfBits) seconds = 0.000000000232831, where the numberOfBits is 4 Bytes * 8 bits/Byte = 32 bits. So the number of microseconds (6 decimal digits), for example, is: milliSecs = $value2 * 0.000232830643654. But displaying more than about 3 digits probably implies more accuracy than is true.
Instead of the MsgBox i want the script to read a .txt file and write to the end of each line the result of a variable, in my case is $FileSize
Here is the code for autoit script
#include <MsgBoxConstants.au3>
#include <File.au3>
Test()
Func Test()
For $i = 1 to _FileCountLines(#TempDir & "\myfiles.txt")
$mread = FileReadLine(#TempDir & "\myfiles.txt", $i)
Local $FileSize = FileGetSize($mread)
MsgBox($MB_SYSTEMMODAL, "", ByteSuffix($FileSize)) ;this is just a test
Next
EndFunc ;==>Example
Func ByteSuffix($Bytes)
Local $Index = 0, $aArray = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']
While $Bytes > 1023
$Index += 1
$Bytes /= 1024
WEnd
Return Round($Bytes) & $aArray[$Index]
EndFunc
Here is the content of .txt file
C:\Users\G-PC\Documents\setup.exe
C:\Users\G-PC\Documents\config.ini
C:\Users\G-PC\Documents\image001.jpg
C:\Users\G-PC\Documents\image002.jpg
C:\Users\G-PC\Documents\image003.jpg
I want the following result
C:\Users\G-PC\Documents\setup.exe [SIZE FOR THIS]
C:\Users\G-PC\Documents\config.ini [SIZE FOR THIS]
C:\Users\G-PC\Documents\image001.jpg [SIZE FOR THIS]
C:\Users\G-PC\Documents\image002.jpg [SIZE FOR THIS]
C:\Users\G-PC\Documents\image003.jpg [SIZE FOR THIS]
There is a size limit for both string variables and arrays, so you have to process one line at a time when working with big files. This uses a temporary file for output instead of keeping all the information in memory. You can rename (overwriting the original file) the temporary file to the original file name at the end.
Func Test()
Local $InFile = FileOpen(#TempDir & "\myfiles.txt", $FO_READ)
Local $OutFile = FileOpen(#TempDir & "\myfiles.tmp", $FO_OVERWRITE)
While True
$File = FileReadLine($InFile)
If #error = -1 Then Return ; no more lines to process
FileWrite($OutFile, $File & " [" & ByteSuffix(FileGetSize($File)) & "]" & #CRLF)
WEnd
EndFunc ;==>Test
$sResult = Test()
ConsoleWrite('SIZE LIST' & #CRLF & $sResult)
Func Test()
Local $FileSize, $sReturn = ''
For $i = 1 to _FileCountLines(#TempDir & "\myfiles.txt")
$mread = FileReadLine(#TempDir & "\myfiles.txt", $i)
$FileSize = FileGetSize($mread)
; MsgBox($MB_SYSTEMMODAL, "", ByteSuffix($FileSize)) ;this is just a test
$sReturn &= $mread & " [" & ByteSuffix($FileSize) & "]" & #CRLF
Next
Return $sReturn
EndFunc ;==>Test
But a better way instead of reading the text file line by line is the using of _FileReadToArray. Than you can iterate through the array with text lines.
Using line number parameter for FileReadLine in a loop resets
the file pointer to the start and scans up to the line number which slows
down the loop as line count increases. Omit that parameter.
Use a file handle so you do not keep on opening and closing
the file with each line read in the loop.
#include <MsgBoxConstants.au3>
#include <File.au3>
$sResult = Test()
MsgBox(0, #ScriptName, $sResult)
Func Test()
Local $iFileSize, $hFile, $sFilePath, $sResult
; Open a file handle to read.
$hFile = FileOpen(#TempDir & "\myfiles.txt")
If $hFile = -1 Then
MsgBox(0x30, #ScriptName, 'Unable to open file to read.')
Exit 1
EndIf
While 1
; Read and let AutoIt handle line count.
$sFilePath = FileReadLine($hFile)
If #error Then ExitLoop
$iFileSize = FileGetSize($sFilePath)
If #error Then ContinueLoop
$sResult &= StringFormat('%s %s\r\n', $sFilePath, ByteSuffix($iFileSize))
WEnd
FileClose($hFile)
Return $sResult
EndFunc
Func ByteSuffix($Bytes)
Local $Index = 0
Local $aArray = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']
While $Bytes > 1023
$Index += 1
$Bytes /= 1024
WEnd
Return Round($Bytes) & $aArray[$Index]
EndFunc
I'm in the process of making a small program to tell me on the fly how many employees are currently punched in via AvayaCMS Reporting.
Right now, the way that it is setup is that I have an Avaya script to take a quick report and export it in a CSV that is used by my Autoit script.
In terms of debugging it, I feel like I missing something and need another set of eyes.
launching the Staffing.au3 triggers the CSV script I'm using against the report. Even having the exact same data my message box still reports "0"
#include <Array.au3>
#include <CSV.au3>
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
$av_LiveData = _ParseCSV($s_ReportSource)
If #error Then Return -1
$av_NameList = _ParseCSV($s_NameList)
If #error Then Return -1
Local $i_AgentCount = 0
For $i_RowCnt = 1 To (UBound($av_LiveData, 1) - 1) Step +1
For $i_AgtLst = 1 To (UBound($av_NameList) - 1) Step +1
If StringStripWS($av_LiveData[$i_RowCnt][1], 3) = StringStripWS($av_NameList[$i_AgtLst][0], 3) Then
$i_AgentCount += 1
EndIf
Next
Next
;Return the Agent Count
Return $i_AgentCount
EndFunc
Name List.csv
Agent Name
"Doe, Jane"
"Doe, John"
Report.csv
,Agent Name,Login ID,Extn,AUX Reason,State,Split/Skill,Time,VDN Name
5,"Doe, John",5930001,1000001,7,AUXOUT,999,51:32:00,
2,"Doe, Jane",5930002,1000002,7,AUXOUT,999,52:32:00,
Tested! It works well with the files supplied (copied them and names them as in your script)
please note the followings
_ParseCsv() has been rewritten add parameters like $Dchar as delimeter etc. (see script)
Note how I locate the names in the file (you can easily add feature to extend search but it won't be becessary)
msgboxes are for explanatory purposes; comment them out if not need them anymore
array.au3 is not needed
and the code:
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
$av_LiveData = _ParseCSV($s_ReportSource,",","oops...",True)
If #error Then Return -1
$av_NameList = _ParseCSV($s_NameList,",","oops: 2",True)
If #error Then Return -1
MsgBox(0,default,"$av_NameList (Number of lines) -> " & $av_NameList[0][0])
MsgBox(0,default,"$av_NameList (Number of data in each line) -> " & $av_NameList[0][1])
MsgBox(0,default,"$av_LiveData (Number of lines) -> " & $av_LiveData[0][0])
MsgBox(0,default,"$av_LiveData (Number of data in each line) -> " & $av_LiveData[0][1])
Local $i_AgentCount = 0
for $i = 1 to $av_NameList[0][0] Step 1
For $j= 1 To $av_LiveData[0][0] Step 1
;we can have names from $av_NameList as well from $av_LiveData but in Live Data 2nd abd 3rd values are the names
MsgBox(0,default,$av_NameList[$i][1] & $av_NameList[$i][2] & " <------------> " & $av_LiveData[$j][2] & $av_LiveData[$j][3])
;~ let's compare them if matched with any of the liveData lines
If StringCompare($av_NameList[$i][1] & $av_NameList[$i][2],$av_LiveData[$j][2] & $av_LiveData[$j][3]) == 0 Then
$i_AgentCount += 1
MsgBox(0,default,"Match found: counter: " & $i_AgentCount)
EndIf
Next
Next
;Return the Agent Count
Return $i_AgentCount
EndFunc
Func _ParseCSV($f,$Dchar,$error,$skip)
Local $array[500][500]
Local $line = ""
$i = 0
$file = FileOpen($f,0)
If $file = -1 Then
MsgBox(0, "Error", $error)
Return False
EndIf
;skip 1st line (since it is the header)
If $skip Then $line = FileReadLine($file)
While 1
$i = $i + 1
Local $line = FileReadLine($file)
If #error = -1 Then ExitLoop
;skip 1st line
$row_array = StringSplit($line,$Dchar)
If $i == 1 Then $row_size = UBound($row_array)
If $row_size <> UBound($row_array) Then MsgBox(0, "Error", "Row: " & $i & " has different size ")
$row_size = UBound($row_array)
$array = _arrayAdd_2d($array,$i,$row_array,$row_size)
WEnd
FileClose($file)
$array[0][0] = $i-1 ;stores number of lines
$array[0][1] = $row_size -1 ; stores number of data in a row (data corresponding to index 0 is the number of data in a row actually that's way the -1)
Return $array
EndFunc
Func _arrayAdd_2d($array,$inwhich,$row_array,$row_size)
For $i=1 To $row_size -1 Step 1
$array[$inwhich][$i] = $row_array[$i]
Next
Return $array
EndFunc
#region ;************ Includes ************
#include "csv.au3"
#include <Array.au3>
#endregion ;************ Includes ************
$i_AgentCount = AvayaData(#ScriptDir & '\Report.csv', #ScriptDir & '\Name List.csv')
MsgBox(0x40, "", $i_AgentCount)
Func AvayaData($s_ReportSource, $s_NameList)
Local $i_AgentCount = 0
Local $s = FileRead($s_NameList)
Local $loggedInNames = FileRead($s_ReportSource)
$av_NameList = _ParseCSV($s_NameList)
If #error Then Return -1
;~ _ArrayDisplay($av_NameList)
ConsoleWrite($s & #CRLF)
For $i = 1 To UBound($av_NameList) - 1
ConsoleWrite($i & " " & $av_NameList[$i][0] & #CRLF)
If StringInStr($s, $av_NameList[$i][0]) <> 0 Then
$i_AgentCount += 1
EndIf
Next
Return $i_AgentCount
EndFunc ;==>AvayaData
I have a text file contains nearly 20 lines wanted to search a string in file and print next 5th line in file using autoit, any one can help me to solve this
#include <File.au3>
#include <array.au3>
$file = #ScriptDir & "\file.txt"
$search = "str"
If FileExists($file) Then
$contents = FileRead($file)
If #error Then
MsgBox(0, 'File Error', $file & ' could not be read.')
Else
For $i = 1 To $count
If StringInStr($contents, $search) Then
MsgBox(0, 'Positive', $file & ' does contain the text "' & $search & '"')
Else
MsgBox(0, 'Negative', $file & ' does NOT contain the text "' & $search & '"')
EndIf
Next
EndIf
EndIf
This reads the text file until the search string is found and then writes the next 5 lines to STDOUT:
#include <File.au3>
#include <Array.au3>
Global $file = #ScriptDir & "\file.txt", $search = "str"
Global $iLine = 0, $sLine = ''
Global $hFile = FileOpen($file)
If $hFile = -1 Then
MsgBox(0,'ERROR','Unable to open file for reading.')
Exit 1
EndIf
; find the line that has the search string
While 1
$iLine += 1
$sLine = FileReadLine($hFile)
If #error = -1 Then ExitLoop
; $search found in the line, now write the next 5 lines to STDOUT
If StringInStr($sLine, $search)And Not $iValid Then
For $i = $iLine+1 To $iLine+5
ConsoleWrite($i & ':' & FileReadLine($hFile, $i) & #CRLF)
Next
ExitLoop
EndIf
WEnd
FileClose($hFile)
Edit
Due to Matt's argument here's a 2nd version of the loop that doesn't use the "line" parameter for FileReadLine.
#include <File.au3>
#include <Array.au3>
Global $file = #ScriptDir & "\file.txt", $search = "str"
Global $iLine = 0, $sLine = '', $iValid = 0
Global $hFile = FileOpen($file)
If $hFile = -1 Then
MsgBox(0,'ERROR','Unable to open file for reading.')
Exit 1
EndIf
; find the line that has the search string
While 1
$iLine += 1
$sLine = FileReadLine($hFile)
If #error = -1 Then ExitLoop
; test the line for the $search string until the flag $iValid is set
If StringInStr($sLine, $search) And Not $iValid Then
$iValid = 1
ContinueLoop
EndIf
If $iValid Then
$iValid += 1
ConsoleWrite($iLine & ':' & $sLine & #CRLF)
If $iValid > 5 Then ExitLoop
EndIf
WEnd
FileClose($hFile)
You won't note much difference between those two versions of the script, unless you are reading a file with 10k+ lines and the lines you are looking for are in the last quarter of that file but it surely is a good idea to prevent possible performance issues.