 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Fri Mar 19, 2010 3:35 pm Post subject: FileRead Project |
|
|
Here is my second project of the day that I'm working on...
I'm attempting to make a gui that will read every six lines of .txt doc. via a button. The code below shows 4 buttons. Here is what I would like the relevent ones to do:
Next: Read the 6th line in the .txt, and when pressed again read the 12th line and so on.
Back: Opposite of the Next button. Read the 12th line, then when pressed again read the 6th line.
Submit: If the user decides the 6th line is what they want to delete, it will delete lines 6 and 7 from the .txt doc.
I'm assuming FileAppend will have to come into play here somewhere, but I have no idea how to accomplish this. Any help would be greatly appreciated.
| Code: | Gui, Add, Text, x12 y10 w330 h30 , Locate the text you wish to delete.
Gui, Add, Edit, x12 y40 w330 h20 vtext,
Gui, Add, Button, x12 y70 w100 h30 , Cancel
Gui, Add, Button, x122 y70 w100 h30 , Back
Gui, Add, Button, x232 y70 w100 h30 , Next
Gui, Add, Button, x122 y120 w100 h30 , Submit
GuiControl, Disable, Edit1
Gui, Show, w358 h166, Some Title
Return
ButtonCancel:
GuiClose:
ExitApp
ButtonBack:
Gui, Submit, NoHide
MsgBox, Back button
Return
ButtonNext:
Gui, Submit, NoHide
FileReadLine, var, file.txt, 6
ControlSetText, Edit1, %var%
Return
ButtonSubmit:
Gui, Submit, NoHide
MsgBox, SumbitButton
Return |
_________________ -Jeremiah |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Fri Mar 19, 2010 4:00 pm Post subject: |
|
|
First of all, holy S T! This rocks! Thank you.
Now, I like this line from the lib...
| Code: | | ("2+3", 150) ; start with line 2, increment 3 upto line 150 so 2,5,8,11,14 etc |
For me, I would probably change it to...
| Code: | | ("6+6", 150) ; What happens if there isn't 150 lines and there's no way of telling how many lines there are? |
Question: Where is the variable that is stored from this command? _________________ -Jeremiah |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Mar 19, 2010 4:10 pm Post subject: |
|
|
Just remove the 150 e.g. don't pass on an endline value as parameter and it should go on to the end of the file no matter how long so like this:
| Code: | Lines:=TF_Readlines("text.txt", "6+6")
MsgBox % Lines |
So use var:=TF_Readlines to assign the read lines to a variable _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Fri Mar 19, 2010 4:14 pm Post subject: |
|
|
So, here's what I have so far. And, so far I'm not having any luck. Perhaps you could help me out?
| Code: | ButtonNext:
Gui, Submit, NoHide
#Include tf.ahk
var:=TF_ReadLines("file.txt", "6+6")
ControlSetText, Edit1, %var%
Return
ButtonSubmit:
Gui, Submit, NoHide
#Include tf.ahk
TF_RemoveLines(var, 6, 7)
Return |
_________________ -Jeremiah |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Mar 19, 2010 4:28 pm Post subject: |
|
|
| Code: | ; http://www.autohotkey.com/forum/viewtopic.php?p=340861#340861
; #include tf.ahk ; only once no need to do it for each button
Gui, Add, Text, x12 y10 w330 h30 , Locate the text you wish to delete.
Gui, Add, Edit, x12 y40 w330 h260 vtext,
Gui, Add, Button, x12 y310 w100 h30 , Cancel
Gui, Add, Button, x122 y310 w100 h30 , Back
Gui, Add, Button, x232 y310 w100 h30 , Next
Gui, Add, Button, x122 y345 w100 h30 , Submit
GuiControl, Disable, Edit1
Gui, Show, w358 h380, Some Title
Return
ButtonBack:
Gui, Submit, NoHide
MsgBox, Back button
Return
ButtonNext:
Gui, Submit, NoHide
var:=TF_ReadLines("text.txt", "6+6") ; this will read lines 6 12 18 etc into a variable
StringReplace, var, var, `n, `r`n, all ; to fix view for edit control as it expects `r`n to separate each line rather than a single `n
ControlSetText, Edit1, %var%
Return
ButtonSubmit:
Gui, Submit, NoHide
MsgBox, 4,Are you sure, This will remove line 6 from Text.txt and overwrite the original file
IfMsgBox Yes
TF_RemoveLines("!text.txt", 6, 6) ; this will operate on the file not the variable
Return
Esc::
ButtonCancel:
GuiClose:
ExitApp
| Just to see what happens add line numbers to your test file so you can see what is going on. _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Fri Mar 19, 2010 4:50 pm Post subject: |
|
|
HAHA! This works fricken perfect! Beautiful! Thank you!
Would you mind helping me out on a bit more with the functionality?
Here are some of the issue/questions I have:
1 How can we get the Next and Back buttons to carry onto the next or previous 6th lines?
2 When the "ControlSetText, Edit1, %var%" happens, it places a 2 after it. This is odd I thought. Any ideas here? _________________ -Jeremiah |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Mar 19, 2010 5:35 pm Post subject: |
|
|
| Jeremiah wrote: | | 1 How can we get the Next and Back buttons to carry onto the next or previous 6th lines? |
Something like this ?
| Code: | ; http://www.autohotkey.com/forum/viewtopic.php?p=340861#340861
; #include tf.ahk ; only once
Line=0
counter=0
Gui, Add, Text, x12 y10 w330 h30 , Locate the text you wish to delete.
Gui, Add, Edit, x12 y40 w330 h260 vtext,
Gui, Add, Button, x12 y310 w100 h30 , Cancel
Gui, Add, Button, x122 y310 w100 h30 , Back
Gui, Add, Button, x232 y310 w100 h30 , Next
Gui, Add, Button, x122 y345 w100 h30 , Submit
GuiControl, Disable, Edit1
Gui, Show, w358 h380, Some Title
Return
ButtonBack:
Gui, Submit, NoHide
MsgBox, Back button
Return
ButtonNext:
Gui, Submit, NoHide
line += 6
var:=TF_ReadLines("text.txt", line, line, 1) ; read only one line, do not add new line
StringReplace, var, var, `n, `r`n, all ; to fix view for edit control
ControlSetText, Edit1, %var%
Return
ButtonSubmit:
Gui, Submit, NoHide
MsgBox, 4,Are you sure?, This will remove line number %line% from Text.txt and overwrite the original file
IfMsgBox Yes
TF_RemoveLines("!text.txt", line, line)
; note line 12 has become 11 now that 6 is removed so may wish to keep track of that as well by using an extra variable for how many lines you have deleted like so:
Counter++
Line -= Counter ; make sure it is line 12 from the orginal order
Gosub ButtonNext ; update view
Return
Esc::
ButtonCancel:
GuiClose:
ExitApp
|
| Jeremiah wrote: | | 2 When the "ControlSetText, Edit1, %var%" happens, it places a 2 after it. This is odd I thought. Any ideas here? | No idea, is there a 2 in the text file? _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Mar 19, 2010 5:39 pm Post subject: |
|
|
So if I understand you correctly you want to view every sixth line of a file and decide to keep/delete correct? in that case a listview gui with checkboxes might be more convenient. That way you have every 6th line in view and can tick a box of those you want to delete and submit the entire batch for deletion in one go.
See http://www.autohotkey.com/docs/commands/ListView.htm with the "checked" option _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Fri Mar 19, 2010 6:52 pm Post subject: |
|
|
| hugov wrote: | Jeremiah wrote:
2 When the "ControlSetText, Edit1, %var%" happens, it places a 2 after it. This is odd I thought. Any ideas here?
No idea, is there a 2 in the text file? |
No, there isn't a 2 in the whole doc as a matter of fact.
| hugov wrote: | | So if I understand you correctly you want to view every sixth line of a file and decide to keep/delete correct? in that case a listview gui with checkboxes might be more convenient. That way you have every 6th line in view and can tick a box of those you want to delete and submit the entire batch for deletion in one go. |
So, do you mean to just use FileReadLine for that then? How would I get it to overwrite the file if the user selects a line to delete? Would I still use the TF lib? _________________ -Jeremiah |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Fri Mar 19, 2010 7:27 pm Post subject: |
|
|
I get the feeling I'm going in the wrong direction here. What do you think?
| Code: | FileReadLine, 6, file.txt, 6
FileReadLine, 10, file.txt, 10
FileReadLine, 14, file.txt, 14
FileReadLine, 18, file.txt, 18
FileReadLine, 22, file.txt, 22
Gui, Add, Radio, x32 y40 w190 h20 vvar6 %6%
Gui, Add, Radio, x32 y60 w190 h20 vvar10, %10%
Gui, Add, Radio, x32 y80 w190 h20 vvar14, %14%
Gui, Add, Radio, x32 y100 w190 h20 vvar18, %18%
Gui, Add, Radio, x32 y120 w190 h20 vvar22, %22%
Gui, Add, Button, x72 y150 w100 h30 , Delete
Gui, Show, w249 h205, Title
Return
GuiClose:
ExitApp
ButtonDelete:
Gui, Submit, NoHide
if var6 = 1
{
msgbox, delete line %6%?
}
if var10 = 1
{
msgbox, delete line %10%?
}
if var14 = 1
{
msgbox, delete line %14%?
}
Return |
_________________ -Jeremiah |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Mar 19, 2010 11:08 pm Post subject: |
|
|
Just tick the boxes of the lines you want to delete, hit submit ...
| Code: | ; www.autohotkey.com/forum/viewtopic.php?p=340861#340861
; #include tf.ahk ; only once
ToRead=2,4,8,9 ; comma separated list of lines you want to view and possibly delete
StringSplit, ToReadIndex, ToRead, `, ; need this in listview as well
Lines:=TF_Readlines("text.txt", ToRead, "", 1) ; read lines
Gui, add, listview , w490 h200 Checked, #|Text
Loop, parse, lines, `n, `r
LV_Add("", ToReadIndex%A_Index%, A_Loopfield) ; fill listview with checbox, orginal line number + text of line
Gui, add, button, , submit
Gui, show, w510 h250, ViewLines
Return
ButtonSubmit:
Delete= ; see LV_GetNext documentation for more info
RowNumber = 0 ; This causes the first loop iteration to start the search at the top of the list.
Loop
{
RowNumber := LV_GetNext(RowNumber, "Checked") ; Resume the search at the row after that found by the previous iteration.
if not RowNumber ; The above returned zero, so there are no more selected rows.
break
LV_GetText(Text, RowNumber)
Delete .= Text "," ; build list of lines to delete e.g. checked lines
}
StringTrimRight, Delete, Delete, 1 ; remove trailing ,
MsgBox, 4, Are you sure?, This will remove lines %Delete% from Text.txt and overwrite the original file
IfMsgBox Yes
TF_RemoveLines("!text.txt", Delete) ; delete selected lins
Return
Esc::ExitApp |
_________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Tue Mar 23, 2010 4:17 pm Post subject: |
|
|
First of all, I don't think I thanked you for this one hugov. This rocks. I love it. However, It doesn't seem to read the whole text file. It seems to have skipped the very 1st line, the 3rd line, then it stops. Any ideas? Thanks again, very much! _________________ -Jeremiah |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Tue Mar 23, 2010 6:15 pm Post subject: |
|
|
| Jeremiah wrote: | | It seems to have skipped the very 1st line, the 3rd line, then it stops. | Not sure what you mean exactly, but TF_Readlines does exactly that, read specific lines so the entire file isn't read as such. Do you want to view all the lines? _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Tue Mar 23, 2010 6:37 pm Post subject: |
|
|
Actually, yeaah I'm dumb. I didn't look close enough at the top where the variable ToRead is set. I just had to edit this and I'm golden. Thanks.
The lines to be read are only for the user to view. The lines that are getting deleted are the one the user selected from the list, and one line before it, and one line after it. Using your code, I am unable to find a way to accomplish this without completely screwing it up. Any help you have would be great. Thank you!
Ex: Line 6 is displayed to user for a delete option.
User decides to delete line six and presses "submit"
The script deletes lines 5-7. _________________ -Jeremiah |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|