thewind27 Posted October 28, 2011 Posted October 28, 2011 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
Ray Posted October 28, 2011 Posted October 28, 2011 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.
Moderators Melba23 Posted October 28, 2011 Moderators Posted October 28, 2011 thewind27, Welcome to the AutoIt forum. One of the useful things about FileReadLine is that you do not need to know how many lines there are! 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. 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. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
thewind27 Posted October 28, 2011 Author Posted October 28, 2011 $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
thewind27 Posted October 28, 2011 Author Posted October 28, 2011 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"
Palmerj Posted October 28, 2011 Posted October 28, 2011 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)
Moderators Melba23 Posted October 28, 2011 Moderators Posted October 28, 2011 thewind27, it just doesn't openAnd how do you know that? Nothing happens on screen, you just get a handle returned. 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. Why not post your full script including the "Series" code and I will take a look. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
thewind27 Posted October 28, 2011 Author Posted October 28, 2011 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
thewind27 Posted October 28, 2011 Author Posted October 28, 2011 (edited) Here is the WHOLE SCRIPT THAT I HAVE BEEN DOING expandcollapse popup$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 October 28, 2011 by thewind27
kaotkbliss Posted October 28, 2011 Posted October 28, 2011 (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"') thenFor $i = 1 To Ubound($line)-1;Do StuffNext *edit*Missed a quote Edited October 28, 2011 by kaotkbliss 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy!
Moderators Melba23 Posted October 28, 2011 Moderators Posted October 28, 2011 (edited) thewind27,I think determining the number of line in Label List.txt and use For...Next is more preciseFor what you want to do you are quite right - beats me why you wanted to use FileReadLine in the first place. Just use _FileCountLines and a For...Next loop. Do not forget to include File.au3. M23Edit: Typnig! Edited October 28, 2011 by Melba23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
kaotkbliss Posted October 28, 2011 Posted October 28, 2011 Just use _FileCountLines and a For...Next loop. Do not forget to include File.au3[i/]. Ooo, didn't even know _FileCountLines existed (or maybe I just forgot about it) 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy!
Moderators Melba23 Posted October 28, 2011 Moderators Posted October 28, 2011 kaotkbliss, Worth the odd scan through the function list in the Help file from time to time - always something new and useful pops up. m23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
thewind27 Posted October 28, 2011 Author Posted October 28, 2011 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
Moderators Melba23 Posted October 28, 2011 Moderators Posted October 28, 2011 thewind27, Run this and see what you get returned from the function: #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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
thewind27 Posted October 28, 2011 Author Posted October 28, 2011 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
Moderators Melba23 Posted October 28, 2011 Moderators Posted October 28, 2011 thewind27,I dont think that's because the File.au3I 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. So please post the script which gave you the problem as it is difficult to debug without code. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
thewind27 Posted October 28, 2011 Author Posted October 28, 2011 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
Moderators Melba23 Posted October 28, 2011 Moderators Posted October 28, 2011 thewind27, Glad you got it working and delighted I could help. Enjoy your AutoIt coding - you will always get help here if you show the effort yourself. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now