Sign in to follow this  
Followers 0
Autoxics

Replcae A Word That Begins With A Specific Character

27 posts in this topic

Hello people!

well i need some of your help with this script i am having problem with, actualy i dont really know where to begin with this script, what to use if i may say so,but here is what id like it to do,

at runtime open a text refference file that has a word in it, the word in this refference file begins with an # as an example and i need the script to read this reff files word and search for this word that begins with an # character and replace the rest of this word, an example the word in the refference file is #helloiamtherestofthisword , i need the script to look for any word that begins with this character # that one and ones it finds this word with no spaces in it i want the script to replace this word #helloiamtherestofthisword as an example with another word that begins with this specific character # and only replace that specific word that begins with that character, i hope you get what iam trying to explain here!i know its confusing lol,

like some kind of string replace,not exactly a string a word if i may say so, and use the refference file to find that specific word that the reff file contains , allso i would want it to read another refference file that has the word it should be replaced with,

like this

One single script reading one reff file for finding a specific word and another reff file with a specific word that should replace the old word.

this should be done with a fileopen Dialog, and that part i have taken care of but , its all to confusing for me on how to achive to get the script to read the reff files while it has opened the file that i want to find and replcae these words in,

For the replacement of the words i tried to use a File read and file Write but i just continue getting errors and it does not exactly do what i want it to do, witch is to search for a specific character and the rest of the word that follows with out spaces,like this #test not like this # test

any help is appreciated!

Share this post


Link to post
Share on other sites



probaly using stringreplace, check the help file :think:

Share this post


Link to post
Share on other sites

Yes thanks ive tried that one aswell, just i cant get the grip on it,

$text = StringReplace("#old", " ", "#new")
$numreplacements = @extended
MsgBox(0, "New string is", $text)
MsgBox(0, "The number of replacements done was", $numreplacements)

its to confusing, i tried to get the #old display #new in this message box example to try to figure it out, i just dont seam to get it right, no mather how i try it,

allso iam trying to get the script to read 2 refference files that has the old word and one reff file that contains the new word that begins with this character #

Share this post


Link to post
Share on other sites

I think am using the wrong marco or somthing in this part, but iam not sure here, allso the mix of all this is hard to get a grip on,

Share this post


Link to post
Share on other sites

Where is the problem here?

i keep trying different things no mather what iam stuck with this unworking script

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
$file = StringReplace("#oldword","#oldword", "#newword",1)
If @error = -1 Then ExitLoop
 Wend
FileClose($file)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

ur opening a file but your not doing anythign iwth it till u close it, and ur opening it with only reading capabilities, from my experience you dont even need to open it and ur not even reading it so its hard to know whats in it so yeah,

$file = Fileread("test.txt")
$string = StringReplace($file, "#oldword", "#newword")
Fileopen("test.txt",2)
FileWrite("test.txt",$string)

heheh it works i was thinking i was forgetting the entire other file contents but infact it does work

Edited by thatsgreat2345

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I got it working, i changed some tiny parts in the code you gave me and it worked great.

hey thank you very much :think:

i saw the reason for the deletion and the error problem that i first got with your code but then i figurd you just forgot to tell me that part,this is how the working script looks like now, A big thank you, allso if its not to much to ask what subject in the help file should i look for to figure out the rest of the script iam writing , witch i explained in the first post,

thanks again!

$file = FileRead("test.txt",0)
$String = StringReplace("#oldword", "#oldword", "#newword")
Fileopen("test.txt",2)
FileWrite("test.txt",$string)
Edited by Autoxics

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

when you run it is it in the same place as your file test.txt if its not then try this

$open = FileOpenDialog("Open TXT file",@scriptdir,"Txt Files (*.txt)")
$file = Fileread($open)
$string = StringReplace($file, "#oldword", "#newword")
Fileopen($open,2)
FileWrite($open,$string)
Edited by thatsgreat2345

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

No problem ive posted above about the script working, i hade the file in the same folder, actualy not in a folder on drive c:\ does not really count as a folder does it!anyway its working the way id hoped for, and iam really happy, i just need some smal directions on how to figure out the rest, what part in the help file , could help me with the file reading the oldword and the newword from refference files .

il give it a go and see if i can figure that part out myself, if not i hope you talent guys can help me with that if i get stuck, thanks soo much for the help with this part, witch i think will help me in the rest of my script, if i just experiment some with what i got now,

Edited by Autoxics

Share this post


Link to post
Share on other sites

um well i would suggest instead of writing it to the same file but when you do a filewrite, write it to a new file and youll just have to do like a filread for the old one and show that in like a messagebox and then read the new one and show that in the message box

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

thank you , i think you just got my mind spinning in the right direction, first i figure that this dident sound to good, but after thinking about it for a minute i realised you are right about what your telling me to do,

the reason i want this script to read the oldword and the newword from a refference file is really because i have a gui that contains 2 inputboxes , one box where i would type the oldword the word i want to replace and the other inputbox is for the newword witch will replace the oldword , and ones this info is typed in the inputboxes, i want the words in the input boxes to automaticly be saved as reff1.txt for the oldword and reff2.txt for the newword ,and this script that iam working on here witch you helped me with is supposed to read those files, ,but i think ive gott the hang on how to achive that now.

thank you again,

Edited by Autoxics

Share this post


Link to post
Share on other sites

NP just post if you need anyhelp , i would also suggest that maybe having an edit box that would have both b4 and afters in 1 edit box and 1 in the other

Share this post


Link to post
Share on other sites

Il, try it out! :think:

Share this post


Link to post
Share on other sites

Hello again, everything ecept one part is working as it should, i first dident notice it, but i soon realised that

the script you gave me, does not keep the current text that is allso in my test .txt file

like this

my test.txt file contains this as an example "Hello iam the #oldword , please replace us #oldword"

so that it my testfile ends up like this "Hello iam the #newword , please replace us #newword"

after ive runned the script that searches for this word "#oldwrod" and has replaced it with this newword "#newword"

insted it kind of deletes the #oldword and writes the #newword , witch is not really a good thing for me since it erases everything ellse in the test.txt file aswell,

maby this script just needs some tweaking for it to do what iam after exactly, but in what way

So the answer iam in need of

How can i keep everything ellse? all other words on its current line and in its current location in the file?

and just replace the specific oldword with the newword , so that the newword takes the oldwords exact place in the file where its being replaced in and allso keeps the other text that is in the file.

Hope you can help!

and hope iam not asking to much.

Share this post


Link to post
Share on other sites

if u do a file read and then a filewrite the entire file should be considered the string and the file write will write the modified string , could u paste ur code please

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

hey thank you for staying tuned and trying to help me out here! :think:

this is the code iam using the code i modified

$file = FileRead("test.txt",0)
$String = StringReplace("#oldword", "#oldword", "#newword")
Fileopen("test.txt",2)
FileWrite("test.txt",$string)

but the test.txt file contains this as an example of a file that i will be using this on

"Hello iam the #oldword , please replace us #oldword"

so when i activate the script i would like the result of the above example to be end up like this

"Hello iam the #newword , please replace us #newword"

Edited by Autoxics

Share this post


Link to post
Share on other sites

once againt you arent making the stringreplace read the file

should be

$file = FileRead("test.txt",0)
$String = StringReplace($file, "#oldword", "#newword")
Fileopen("test.txt",2)
FileWrite("test.txt",$string)

Share this post


Link to post
Share on other sites

I just ran your script and everything in the text file gets erased,

i have this text in a text file called test.txt

Hello iam the #oldword , please replace us #oldword

and it turns out with nothing in the resulting text file.

what am i doing wrong here+

Share this post


Link to post
Share on other sites

I would like it to turn out like this

Hello iam the #newword , please replace us #newword

ones ive runed the script on the text file

Share this post


Link to post
Share on other sites

Are you still here ThatsGreat2345?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0