Sign in to follow this  
Followers 0
ArmandoXS

Txt Files

8 posts in this topic

#1 ·  Posted (edited)

i think the topic description is very self explainatory, I was thinking of using FileWriteLine but im not sure on how to remove whats there and then aswell save it

Edited by DarkDragon

Share this post


Link to post
Share on other sites

If you have the name of the file,

and you know the text you want to change,

you can use this simple text replace code

$TextFileName = "test.txt" ; Name of the file
$FindText = "HEllo" ;Text you want to find
$ReplaceText = "Good Bye" ; TExt you want to replace it with

$FileContents = FileRead($TextFileName) ;Reads all the information in the text file
$FileContents = StringReplace($FileContents, $FindText, $ReplaceText) ; reads filecontents, finds the text you want to remove, puts ReplaceText in its place
FileDelete($TextFileName) ; Deletes the file
FileWrite($TextFileName, $FileContents);Remakes the file with the new content

If you want to let the user change the text you want to read and replace, then there is a few more lines you have to add, just reply if this solved your problem or if you want me to go into more complicated stuff.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

If you have the name of the file,

and you know the text you want to change,

you can use this simple text replace code

$TextFileName = "test.txt" ; Name of the file
$FindText = "HEllo" ;Text you want to find
$ReplaceText = "Good Bye" ; TExt you want to replace it with

$FileContents = FileRead($TextFileName) ;Reads all the information in the text file
$FileContents = StringReplace($FileContents, $FindText, $ReplaceText) ; reads filecontents, finds the text you want to remove, puts ReplaceText in its place
[color="#FFFF00"]FileDelete($TextFileName) ; Deletes the file
FileWrite($TextFileName, $FileContents);Remakes the file with the new content

If you want to let the user change the text you want to read and replace, then there is a few more lines you have to add, just reply if this solved your problem or if you want me to go into more complicated stuff.

Crystal Clear Thanks :mellow: exept when its says file delete and file read will the only things that were changed be deleted and rewritten or will the whole text file screw up?? Edited by DarkDragon

Share this post


Link to post
Share on other sites

Nope, It keeps the whole file intact, with the new line just inserted where it should be

Because it reads the contents of the file, replaces the few lines of text then it saves all of the text under FIleContents and when it rewrites it, it used the File Contents to keep it all intact

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

ok... but do i need the filedelete??? im a little confused b/c it appears that ur just deleting the varible itself (or the name of the file , not the actual file)

Edited by DarkDragon

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Honestly, im not sure. I'll look into it a bit more for you.

I'm not a pro at coding in autoit yet, but none the less, the code works.

Edit: You are deleting the file, so you can remake it with the correct information

If you dont delete it, it will just put the redone information under the current information

Edited by Shakala

Share this post


Link to post
Share on other sites

Honestly, im not sure. I'll look into it a bit more for you.

I'm not a pro at coding in autoit yet, but none the less, the code works.

ok thanks for the help :mellow:

Share this post


Link to post
Share on other sites

If you have the name of the file,

and you know the text you want to change,

you can use this simple text replace code

If you want to let the user change the text you want to read and replace, then there is a few more lines you have to add, just reply if this solved your problem or if you want me to go into more complicated stuff.

Hi guys.

I just registered to this forum as this topic is almost exactly what I was looking for and I also wanted to thank you.

However, as I am absolutely new to AutoIt and as this topic only "almost exactly" solves my problem, please allow an additional question to the one by DarkDragon:

Is there a way of finding and replacing whole text blocks, i.e. text that is expanded to several lines within a txt-based file? To be more precisely: I have 2 txt-based files and need to copy one (or more) text blocks from file a to b.

Any help would be appreciated.

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