AutoHotkey Community

It is currently May 26th, 2012, 12:01 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Remove Line contains Var
PostPosted: September 5th, 2008, 1:18 am 
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?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2008, 1:43 am 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2008, 11:36 am 
Offline

Joined: March 23rd, 2005, 7:53 am
Posts: 321
Location: Germany
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2008, 4:05 pm 
Offline

Joined: May 1st, 2007, 8:36 pm
Posts: 83
Location: The Netherlands
@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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2008, 4:08 pm 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
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
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Yahoo [Bot] and 18 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