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 

Search and replace text

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






PostPosted: Wed Nov 18, 2009 3:18 pm    Post subject: Search and replace text Reply with quote

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.
Back to top
hugov



Joined: 27 May 2007
Posts: 2181

PostPosted: Wed Nov 18, 2009 3:44 pm    Post subject: Reply with quote

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.
_________________
Tut 4 Newbies
TF : Text file & string lib, TF Forum
Back to top
View user's profile Send private message Visit poster's website
:?:
Guest





PostPosted: Wed Nov 18, 2009 3:53 pm    Post subject: Reply with quote

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
     }
  }
}
Back to top
Guest






PostPosted: Wed Nov 18, 2009 5:56 pm    Post subject: Reply with quote

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.
Back to top
Guest






PostPosted: Wed Nov 18, 2009 6:46 pm    Post subject: Reply with quote

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.
Back to top
Display posts from previous:   
Post new topic   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