Sign in to follow this  
Followers 0
spyro

Find and replace in a text file

9 posts in this topic

I have look at StringReplace but im not sure how to make it work for a text file. I want to find a certain string in a text file and then replace it.


www.SecurityDistro.com

Share this post


Link to post
Share on other sites



Look in the help at the example for FileReadLine. Just loop through the file, read each line, replace the string if necessary and write to a temp file. When done, delete the original file and rename the temp one.


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

Look in the help at the example for FileReadLine. Just loop through the file, read each line, replace the string if necessary and write to a temp file. When done, delete the original file and rename the temp one.

That is similar to what I tried to do. The problem is that for some reason I have always had trouble getting AutoIt to delete or rename external files correctly. The very last line in my code is the delete call and it still will not delete. I also have a call before that to close the file just to make sure it is ready for deletion.


www.SecurityDistro.com

Share this post


Link to post
Share on other sites

Look at _FileReadToArray and _FileWriteFromArray.

You would read the file in, loop thru the array, replace the text and write back out to the same file.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Look at _FileReadToArray and _FileWriteFromArray.

You would read the file in, loop thru the array, replace the text and write back out to the same file.

I will take a look at that. What do you think of the problem with AutoIt deleting files?


www.SecurityDistro.com

Share this post


Link to post
Share on other sites

What do you think of the problem with AutoIt deleting files?

Are you closing the file before trying to delete it? Is there another process that has the file open?

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

Are you closing the file before trying to delete it? Is there another process that has the file open?

I wrote a test program just to see if there is something I am doing wrong. My guess is that there is.

FileOpen("file.txt",1)

FileWrite("file.txt","check me out")

FileClose("file.txt")

FileDelete("file.txt")

Running this does not delete the file off of my box.


www.SecurityDistro.com

Share this post


Link to post
Share on other sites

It appears that AutoIt does not like me hardcoding the file name. I wrote the following and it worked.

$file = FileOpen("file.txt",1)

FileWrite($file,"check me out")

FileClose($file)

FileDelete("file.txt")


www.SecurityDistro.com

Share this post


Link to post
Share on other sites

It appears that AutoIt does not like me hardcoding the file name. I wrote the following and it worked.

$file = FileOpen("file.txt",1)

FileWrite($file,"check me out")

FileClose($file)

FileDelete("file.txt")

It had nothing to do with "Hard coding", FileClose("file.txt") << was wrong. You need to use the "handle" from FileOpen() to close it, which you did in your last example. Just thought you'd like to know why the last one worked, and the other didn't.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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