AutoHotkey Community

It is currently May 27th, 2012, 3:02 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: FileRead Project
PostPosted: March 19th, 2010, 4:35 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 4:48 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Look at the TF lib: TF_Readlines to read 6th 12th Xth line, TF_RemoveLines to remove etc http://www.autohotkey.net/~hugov/tf-lib.htm

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 5:00 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
First of all, holy S:shock: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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 5:10 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 5:14 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 5:28 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 5:50 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 6:35 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 6:39 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 7:52 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 19th, 2010, 8:27 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 20th, 2010, 12:08 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 23rd, 2010, 5:17 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 23rd, 2010, 7:15 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 23rd, 2010, 7:37 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: rbrtryn and 31 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group