Jump to content

Txt Files


Recommended Posts

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.

Link to comment
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
[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
Link to comment
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

Link to comment
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.

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
Link to comment
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...