AutoHotkey Community

It is currently May 26th, 2012, 11:30 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Search and replace text
PostPosted: November 18th, 2009, 4:18 pm 
Is there any script of command that can help me search and replace text in a text file? I have a script with some inputboxes. What the user writes is stored into variables. Now, I need to add a series of commands to search a specific string in some text files and replace it with those variables.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2009, 4:44 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
If you want to use a ready made solution have a look at TF.ahk A Library for Text file manipulation or http://www.autohotkey.net/~hugov/tf-lib.htm

Functions of interest are TF_(RegEx)Replace(InLines)

If you want to roll your own look at the FileRead, StringReplace, RegExReplace AutoHotkey commands.

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2009, 4:53 pm 
Code:
#NoEnv
SetWorkingDir, %A_ScriptDir%
txt=
(
Some Text
Some Text
Some Text
)
IfExist, %A_ScriptDir%\test.txt
 FileDelete, %A_ScriptDir%\test.txt
FileAppend, % txt, %A_ScriptDir%\test.txt
FileRead, T, %A_ScriptDir%\test.txt
InputBox, S, Search, , , 200, 100 ; SearchStr...
If !ErrorLevel
 InputBox, R, Replace, , , 200, 100 ; Replacement...
If !ErrorLevel
 Replace(T, S, R)
Run, %A_ScriptDir%\test.txt

Replace(T, S, R) {
 FoundPos := RegExMatch(T, S)
 If(FoundPos != "0") {
    NewTxt := RegExReplace(T, S, R)
     IfExist, %A_ScriptDir%\test.txt
     {
      FileDelete, %A_ScriptDir%\test.txt
      FileAppend, % NewTxt, %A_ScriptDir%\test.txt
     }
  }
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2009, 6:56 pm 
This latest script you posted works fine, althought I don't understand well the code. Anyway, could you tell me how to remove the replacement input box and substitute it with a fixed text, for instance, a word, which will always be the same. Thanx.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2009, 7:46 pm 
I don't need any search function. I just need that the script replaces any instance of a word in a txt file (it's always the same word and it will be in the file) with what the user sends through a input box.


Report this post
Top
  
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: Leef_me, Maestr0, Pulover, rbrtryn, XstatyK and 67 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