 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Nov 18, 2009 3:18 pm Post subject: Search and replace text |
|
|
| 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
|
|
| Back to top |
|
 |
:?: Guest
|
Posted: Wed Nov 18, 2009 3:53 pm Post subject: |
|
|
| 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
|
Posted: Wed Nov 18, 2009 5:56 pm Post subject: |
|
|
| 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
|
Posted: Wed Nov 18, 2009 6:46 pm Post subject: |
|
|
| 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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|