AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Remove Line contains Var

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
helpmeplz
Guest





PostPosted: Fri Sep 05, 2008 12:18 am    Post subject: Remove Line contains Var Reply with quote

I need help with a function I'm trying to do.

Basically a variable is going to come in with a header that identifys that this is for deleting.

It will look something like this
Code:

1=a1

; Text File bellow

Line1=~Open~a1~8
Line2=~Open~a2~8

What I want to do is find which line in the text file contains the contents of variable "1" and remove the entire line completely.

I tried
Code:

fileread, in, %textfile%
stringreplace, out, in, %1%`r`n

but it doesnt seem to work

Any other suggestions?
Back to top
Serenity



Joined: 07 Nov 2004
Posts: 1271

PostPosted: Fri Sep 05, 2008 12:43 am    Post subject: Reply with quote

You can use a parsing loop to find and remove the line, and use FileDelete/FileAppend to create the replacement file.

Code:
ifnotexist, example.txt
   fileappend, one`ntwo`nthree`nfour, example.txt ; create file

1=three
fileread, file, example.txt
loop, parse, file, `n
{
   if a_loopfield not contains %1%
      newfile .= (newfile != "" ? "`n" : "") . a_loopfield   
}
filedelete, example.txt
fileappend, %newfile%, example.txt

_________________
"Anything worth doing is worth doing slowly." - Mae West
Back to top
View user's profile Send private message Visit poster's website
Hasso



Joined: 23 Mar 2005
Posts: 321
Location: Germany

PostPosted: Mon Sep 08, 2008 10:36 am    Post subject: Reply with quote

I think it would be no good idea to name a variable as a number. In most programming languages this is forbidden.
_________________
Hasso

Programmers don't die, they GOSUB without RETURN
Back to top
View user's profile Send private message
Wouther



Joined: 01 May 2007
Posts: 83
Location: The Netherlands

PostPosted: Mon Sep 08, 2008 3:05 pm    Post subject: Reply with quote

@Hasso: I agree. Besides, variables named as numbers are used as the script's parameters in ahk.

Here's my small contribution, it being a function, it also returns how many lines where removed:
Code:
MsgBox, % FileRemoveLine("YourFile.txt", "YourText") . " lines have been removed."

FileRemoveLine(paramFile, paramKey)
{
   c := 0, tmp := A_Temp . "\TMP_" . A_ScriptName . "_" . A_TickCount . ".txt"
   Loop, Read, % paramFile, % tmp
      IfNotInString, A_LoopReadLine, % paramKey
         FileAppend, % A_LoopReadLine
      Else c++
   FileMove, % tmp, % paramFile, 1
   Return c
}

_________________
Printing css/html-formatted text
Back to top
View user's profile Send private message
Serenity



Joined: 07 Nov 2004
Posts: 1271

PostPosted: Mon Sep 08, 2008 3:08 pm    Post subject: Reply with quote

If the file is big that is alot of disk writes - one for each line. Reading the file into a variable and parsing it is probably better.
_________________
"Anything worth doing is worth doing slowly." - Mae West
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group