Sign in to follow this  
Followers 0
thewind27

Read how many lines are in a text file

19 posts in this topic

I would line to read how many lines are in a text file. For example, if there are 23 lines in that text file then it will do the loop 23 times.

Please help me with this. I'm stucked

Here is the current code I'm using:

$line = FileReadLine("Label List.txt")
 
For $x = 1 to $line
 
 
"Series of things i would like to do"
 
 
Next

Share this post


Link to post
Share on other sites



First, you need to open the file for reading.

if you are using SciTE, place your cursor on FileReadLine and press the F1 key, look at example script at bottom of the help page.

Share this post


Link to post
Share on other sites

thewind27,

Welcome to the AutoIt forum. :D

One of the useful things about FileReadLine is that you do not need to know how many lines there are! :D

Look at the example in the Help file. You just keep calling the function and it advances line by line automatically. All you have to do is to check when you get an error of -1 as this mean it has read all the lines. :rip:

So your code would read like this:

; Open your file
$hFile = FileOpen("Label List.txt")
 
; Start the infinite loop
While 1
    
    ; Read in the next line - it will start with the first automatically
    $line = FileReadLine($hFile)
    ; Check if we have run out of lines and exit the loop if we have
    If @error = -1 Then ExitLoop
    
    ; Then for each line:
    ; "Series of things i would like to do"
  
WEnd
 
; And finally close the file
FileClose($hFile)

Note that it is much quicker to open the file and use the handle - if you use the full path you have to open and close the file each time. But than you must remember to close it again later. :)

All clear? If not, please ask. :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

$hFile = FileOpen("Label List.txt")

some how this doesn't work on my project. I save this project in the same folder with Label List.txt

it just doesn't open, i tried the full path, same thing happens

Share this post


Link to post
Share on other sites

Also, is there a way to stop do "series of things i want to do" when the Label List.txt is empty?

Eventhou the Label list.txt is empty, your code still do the "Series"

Share this post


Link to post
Share on other sites

Check the file handle to make sure it is NOT -1

$hFile = FileOpen("Label List.txt")
; Start the infinite loop
if $hfile <> -1 Then
While 1
    ; Read in the next line - it will start with the first automatically
    $line = FileReadLine($hFile)
    ; Check if we have run out of lines and exit the loop if we have
    If @error = -1 Then ExitLoop
ConsoleWrite($line & @CRLF)
    ; Then for each line:
    ; "Series of things i would like to do"
WEnd
EndIf
; And finally close the file
FileClose($hFile)

Share this post


Link to post
Share on other sites

thewind27,

it just doesn't open

And how do you know that? Nothing happens on screen, you just get a handle returned. :oops:

So try this and see if it does open:

; Open your file
$hFile = FileOpen("Label List.txt")

; Check if file opened
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file")
    Exit
EndIf

; Start the infinite loop
While 1

    ; Read in the next line - it will start with the first automatically
    $line = FileReadLine($hFile)
    ; Check if we have run out of lines and exit the loop if we have
    If @error = -1 Then ExitLoop

    ; Then for each line:
    ; "Series of things i would like to do"

WEnd

; And finally close the file
FileClose($hFile)

Eventhou the Label list.txt is empty, your code still do the "Series"

No it does not - I have just checked on an empty file. It exits the loop as soon as an error of [-1 is returned - which is when there are no more lines to read. :rip:

Why not post your full script including the "Series" code and I will take a look. :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I think determining the number of line in Label List.txt and use For...Next is more precise

All I need is to know how many lines there are so I can do

For $x = 1 to $number of line

Please help me with this. Thanks

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Here is the WHOLE SCRIPT THAT I HAVE BEEN DOING

$welcome = MsgBox(1, "Welcome to HTH Label Helper", "Please get TrackFlow ready or click Cancel to Exit")
If $welcome = 2 then
MsgBox(0,"HTH Label Helper","The program will now exit. Have a nice day.")
Sleep(2000)
Exit
Else
MsgBox (0, "HTH Label Helper", "Please scan your ENSs into Notepad.")
EndIf
Run("Notepad.exe")
WinWaitActive("[CLASS:Notepad]")
Send("!F")
Send("A")
Sleep(500)
Send("C:\Users\user\Desktop\HTH Label\Label List.txt")
Send("!S")
Send("!Y")
Sleep(100)
$process = MsgBox(0, "Process", "Scan your ESN into the Notepad and press OK when you are done.")
If $process = 1 Then
WinWaitActive("Label List - Notepad")
Send("^S")
EndIf
Send("{HOME}")
Send("{UP 150}")
Sleep(300)
$hFile = FileOpen("C:\Users\user\Desktop\HTH Label\Label List.txt")
 
While 1
    $line = FileReadLine($hFile)
    If @error = -1 Then ExitLoop
Send("+{END}")
Sleep(500)
Send("^x")
Sleep(500)
Send("{DOWN}")
Sleep(500)
Send("{BS}")
Sleep(500)
Send("!{TAB}")
Sleep(500)
MouseClick("primary", 288, 616, 1)
Sleep(500)
Send("^v")
sleep(500)
Send("{ENTER}")
Sleep(500)
Send("!{TAB}")
Sleep(500)
WEnd
 
 
Sleep(2000)
$closelist = MsgBox(4, "Complete","Label Helper completed his job. Do you want to close Label List?")
If $closelist = 6 then
Send("!f")
Send("x")
Send("n")
Endif
Exit
Edited by thewind27

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

if it is reading a blank line and you do not want to have anything done you can try adding

If $line <> "" Then

; Then for each line:

; "Series of things i would like to do"

EndIf

or you could use

$line = _FileReadToArray('"C:\Users\user\Desktop\HTH Label\Label List.txt"')

then

For $i = 1 To Ubound($line)-1

;Do Stuff

Next

*edit*

Missed a quote

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

thewind27,

I think determining the number of line in Label List.txt and use For...Next is more precise

For what you want to do you are quite right - beats me why you wanted to use FileReadLine in the first place. :D

Just use _FileCountLines and a For...Next loop. Do not forget to include File.au3. :oops:

M23

Edit: Typnig!

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Just use _FileCountLines and a For...Next loop. Do not forget to include File.au3[i/]. :D

Ooo, didn't even know _FileCountLines existed (or maybe I just forgot about it)


010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

kaotkbliss,

Worth the odd scan through the function list in the Help file from time to time - always something new and useful pops up. :D

m23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I think I did it right but this is killing me

For $i = 0 to (a number) will always work

but

For $i= 0 to $line is not working it just do the "series" once.

#include<File.au3>
$line = _FileCountLines("C:\Users\user\Desktop\HTH Label\Label List.txt")
For $x = 0 to $line
Send("+{END}")
Sleep(500)
Send("^x")
Sleep(500)
Send("{DOWN}")
Sleep(500)
Send("{BS}")
Sleep(500)
Send("!{TAB}")
Sleep(500)
MouseClick("primary", 288, 616, 1)
Sleep(500)
Send("^v")
sleep(500)
Send("{ENTER}")
Sleep(500)
Send("!{TAB}")
Sleep(500)
Next

Share this post


Link to post
Share on other sites

thewind27,

Run this and see what you get returned from the function: :D

#include<File.au3>
$line = _FileCountLines("C:\Users\user\Desktop\HTH Label\Label List.txt")
MsgBox(0, "Return", "Lines: " & $line & @CRLF & @CRLF & "Error: " & @error)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

That's right. I copied your code into a empty au3 file and Run

I have 4 lines so it read Lines: 4 Error: 0

Back to my situation, now I try to run my script the it show the error:

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\user\Desktop\HTH Label\Label Scan.au3"   
C:\Program Files\AutoIt3\Include\File.au3 (47) : ==> "If" statement has no matching "EndIf" statement.:
Func _FileCountLines($sFilePath)
>Exit code: 1    Time: 0.223

I dont think that's because the File.au3

So what did I do wrong?

Thank you so much

Share this post


Link to post
Share on other sites

thewind27,

I dont think that's because the File.au3

I do - because that is what the error tells me. But as lots of us use this #include file without problem I think there might be another reason. :D

So please post the script which gave you the problem as it is difficult to debug without code. :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thanks M23,

I found the reason going on with my end if and I didn't save the text file before let the _FileCountLines do its job

Once again, Thank you so much for your time to help me out

I really appreciate your help. This is my first auto program that I wrote, and u just gave me motivation.

Thanks

Share this post


Link to post
Share on other sites

thewind27,

Glad you got it working and delighted I could help. :D

Enjoy your AutoIt coding - you will always get help here if you show the effort yourself. :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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