Replace in text file
i need to search and replace multiple occurrence of same string in some text file , can someone point in right direction please?
Tnx
for example:
FileRead, TheText, TextFile.txt
StringReplace, NewText, TheText, Chopin, Mozart, All
In example above, AutoHotkey will read Textfile.txt and save it to TheText variable.
In next step it will use StringReplace to change any "Chopin" it find to "Mozart". It will save into a NewText variable.
Try to use FileRead and StringReplace
for example:
FileRead, TheText, TextFile.txt
StringReplace, NewText, TheText, Chopin, Mozart, All
In example above, AutoHotkey will read Textfile.txt and save it to TheText variable.
In next step it will use StringReplace to change any "Chopin" it find to "Mozart". It will save into a NewText variable.
tnx a lot for quick response :-)
i wrote this:
FileRead, TheText, test.csv StringReplace, NewText, TheText, ',', ';', All msgbox, %NewText% FileDelete, test.csv FileAppend, %NewText%, test.csv
but it doesn't work, maybe i'm doing something wrong with comma and semicolon ?
tnx
2) check out the CSV library <!-- m -->http://www.autohotke...topic55909.html<!-- m -->
3) you need to escape the , with a backtic ` (not ' but a `) in your example
Hi is there a way to tweak this script to fit htm files? For instance:
FileRead, TheText, (Any File Name).htm
The idea is I need to replace a set a characters from each htm file. I have a list of characters to search and replace so would I need a script for each character? Does anyone know if there is a way to search for list of characters within one file and replace them?
Please let me know. Thanks!
You should be able to use skonvols2k's script and change his .csv file in for your .htm file
http://www.autohotke...le/#entry419584
And just do a bunch of StringReplace commands in a row, acting on the same variable like this:
StringReplace, NewText, TheText, (character_X), (with character_A), All StringReplace, NewText, NewText, (character_Y), (with character_B), All StringReplace, NewText, NewText, (character_Z), (with character_C), All
Thank you! Here is what I am trying:
FileRead, TheText, AnyHtmFile.htm (I want this to be able to accept any htm file without me having to enter the title) StringReplace, NewText, TheText, `§`, `§`, All StringReplace, NewText, NewText, `ˆ`, `^`, All StringReplace, NewText, NewText, `ü`, `ü`, All FileDelete, AnyHtmFile.htm (I want this to be able to save/replace the input htm file without me having to do anything) FileAppend, %NewText%, AnyHtmFile.htm (end with the input file being updated after all characters are searched and replaced)
I have to do this process over multiple times to different htm files and I have a list 33 characters to search/replace. I am trying this script but nothing seems to work. Got any ideas?
You've got backticks (`) before commas. If you put them before a comma, that comma counts as escaped, which means it is not dividing parameters in the command but is instead being treated as a literal purposeful comma.
Right now your StringReplace is trying to replace the text "§, §, All" with.. nothing
That's why it doesn't seem to be doing anything, because it doesn't find that string in your variable and won't erase it. Look at this:
string:="Something wicked, this way comes, today §, !" StringReplace, test1, string, `§`, !, All ; This will replace "§, !" with "All" MsgBox % test1 StringReplace, test2, string, `§`, !, ; This will delete "§, !" MSgBox % test2 StringReplace, test3, string, `§`,, ?, All ; This will replace "§," with "?" MsgBox % test3 StringReplace, test4, string, `,, _, All ; This will replace all commas with underscores MsgBox % test4 return
What you instead intended for was `§, `§, All
Thank you! This seems to work:
StringReplace, test3, string, `§`,, §, All ; This will replace "§," with "§" MsgBox % test3
However, I can't get it to read/replace the § within the htm file. All it does is create a blank htm file after the script is executed. I really appreciate all the help!
I was noticing that § in my MsgBox was not rendering right (even though I'm on Unicode AHK). Try using the &#???; ID to represent that § maybe? Or Call it {U+00A7}.
Could you share the entirety of your script as you have it now so we can see what is going wrong? You shouldn't get a blank htm file unless string itself was blank.