Sign in to follow this  
Followers 0
BatMan22

Reading file Line by line, repeat one line at a time till done

7 posts in this topic

#1 ·  Posted

So I needed help cleaning up the mess I've made... So I've been pretty successful in getting what I want done, stuck on the reading of my file that I've created one line at a time. Help please :) I just need to read it and send a message with each lines data from the second line to the end of the file.  I appreciate your help. 

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Date.au3>

Sleep(3000)
Global $aDate, $aTime

MsgBox(0,"", _NowDate ( ))
;File Directory Path - C:\Program Files\Metrohm\IC Net 2.2\IC Net\Systems\HPUV
Send("^c")
$text = ClipGet()
$dashPosition = StringInStr($text,"-")
$sFileName = @ScriptDir &"\" & @MON & @MDAY & @YEAR & ".que" ; Create file in same folder as script

; Prove it exists
If FileExists($sFileName) Then
    $cancelorno=MsgBox(1, "File", "File Already Exists! Hit OK to continue or CANCEL to make sure sequence isn't running")
    If $cancelorno='2' then Break else Filewrite($file,Chr(10))
Else
    MsgBox($MB_SYSTEMMODAL, "File", "Does not exist")
EndIf

; Write all clipboard data
FileWrite($sFileName, $text)


; Read all
MsgBox($MB_SYSTEMMODAL, "File Content", FileRead($sFileName))

;Read One Line At a Time!
$numberoflines = 4 ; temporarily chose 4 until I can figure out function to count number of lines in a text file
For $numberoflines = 1 to $numberoflines
   $eachline = FileReadLine ($sFileName,$numberoflines) ; read $numberoflines down, starting at 1, not 0., last line of page is -1.
   MsgBox(0,"",$eachline)
Next

; Close the file handle
FileClose($sFileName)

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I figured it out but is there a cleaner way to do it? It seems to make an i7 computer lag a bit, and I want to run this on a XP computer from 10+ years back eventually

;Read One Line At a Time!
$numberoflines = _FileCountLines ($sFileName) ; temporarily chose 4 until I can figure out function to count number of lines in a text file
MsgBox(0,"", $numberoflines)
$currentcount = '1'
For $i = 1 to $numberoflines
   $eachline = FileReadLine ($sFileName,$currentcount) ; read $numberoflines down, starting at 1, not 0., last line of page is -1.
   MsgBox(0,"",$currentcount)
   MsgBox(0,"",$eachline)
   $currentcount = $currentcount+'1'
Next

 

Edited by BatMan22

Share this post


Link to post
Share on other sites

#3 ·  Posted

The reason you are seeing lag, the way you have written it is actually spelled out in the help file for FileReadLine:

Quote

From a performance standpoint it is a bad idea to read line by line specifying "line" parameter whose value is incrementing by one. This forces AutoIt to reread the file from the beginning until it reach the specified line.

I typically will write the entire file to an array once, and then iterate through it, something like this:

#include <File.au3>

Local $aArray = FileReadToArray(@DesktopDir & "\Testing.txt")
    For $sLine In $aArray
        ConsoleWrite($sLine & @CRLF)
    Next

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Sweet, thanks so much. Using your code DID get rid of the lag, only problem is I don't know how to get it to either not read the first line into the array at all or just not paste out the first line. I tried the thing below but it didn't work. Basically if I have 10 lines in the file, I want it to start from line 2, not from the first line.

 

Local $aArray = FileReadToArray($sFileName)
    For $sLine + "1" In $aArray
        ConsoleWrite($sLine & @CRLF)
        Send($sLine & " s 7199{Enter}" )
    Next

 

Edited by BatMan22

Share this post


Link to post
Share on other sites

#5 ·  Posted

There are a couple of ways to do it. You could do it like this:

#include <File.au3>

Local $aArray = FileReadToArray(@DesktopDir & "\Testing.txt")
    For $a = 1 To UBound($aArray) - 1
        ConsoleWrite($aArray[$a] & @CRLF)
    Next

Or use _FileReadToArray, which gives you the number of array elements in the [0] index:

#include <File.au3>
Local $aArray
_FileReadToArray(@DesktopDir & "\Testing.txt", $aArray)
    For $a = 2 To $aArray[0]
        ConsoleWrite($aArray[$a] & @CRLF)
    Next

 

1 person likes this

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#6 ·  Posted

Last question for the next 15 minutes, I promise... What did I do wrong here? I want the Cancel button to end program, Yes button to write $text to file after typing enter once, and no to clear or delete the file before pasting data into it.

 

If FileExists($sFileName) Then
    $cancelorno=MsgBox(3, "File", "File Already Exists! Hit OK to continue without clearing / Hit NO to CLEAR then continue or CANCEL to stop program")
    If $cancelorno='2' Then Break ; cancel
    If $cancelorno='7' Then FileDelete($sFilename) & Filewrite($sFilename,"" & $text) ; No should clear then file first then paste data
    If $cancelorno='6' Then Filewrite($sFilename,Chr(10) & $text) ; Yes should appent existing data after typing enter first
Else
    MsgBox($MB_SYSTEMMODAL, "File", "Does not exist")
EndIf

 

Share this post


Link to post
Share on other sites

A little ugly but I got it working :)

If FileExists($sFileName) Then
$return_value = MsgBox(3,"File", "File Already Exists! Hit OK to continue without clearing / Hit NO to CLEAR then continue or CANCEL to stop program",10)
   Switch $return_value
         case 2 ;cancel ---> Flags: 1, 3, 5, 6
            MsgBox(0, "Return Value", "Cancel")
            Break
         case 6 ;Yes ---> Flags: 3, 4
            MsgBox(0, "Return Value", "Yes")
            FileWriteLine($sFilename , $text)
         case 7 ;No ---> Flags: 3, 4
            MsgBox(0, "Return Value", "No")
            FileDelete($sFileName)
            $sFileName = @ScriptDir &"\" & @MON & @MDAY & @YEAR & ".que"
            FileWrite($sFilename, $text)
     EndSwitch

Else
    MsgBox($MB_SYSTEMMODAL, "File", "Does not exist")
    FileWrite($sFileName, $text)
EndIf

 

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