 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
helpmeplz Guest
|
Posted: Fri Sep 05, 2008 12:18 am Post subject: Remove Line contains Var |
|
|
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
|
Posted: Fri Sep 05, 2008 12:43 am Post subject: |
|
|
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 |
|
 |
Hasso
Joined: 23 Mar 2005 Posts: 321 Location: Germany
|
Posted: Mon Sep 08, 2008 10:36 am Post subject: |
|
|
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 |
|
 |
Wouther
Joined: 01 May 2007 Posts: 83 Location: The Netherlands
|
Posted: Mon Sep 08, 2008 3:05 pm Post subject: |
|
|
@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 |
|
 |
Serenity
Joined: 07 Nov 2004 Posts: 1271
|
Posted: Mon Sep 08, 2008 3:08 pm Post subject: |
|
|
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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|