Jump to content

How to write something into an opened file


Recommended Posts

Dear all,

I am trying to write something into an opened file.

I found following functions:

FileWriteLine

FileWrite

But these two functions only append text to the end of the opened file.

Function _FileWriteToLine can write to a specific line. But this is not what I want.

I want to read a file, if I found some specific string, I will insert some string to that location. Just like the function C language provided. Is there any functions in AutoIt that can do it?

Thanks.

Link to comment
Share on other sites

Dear all,

I am trying to write something into an opened file.

I found following functions:

FileWriteLine

FileWrite

But these two functions only append text to the end of the opened file.

Function _FileWriteToLine can write to a specific line. But this is not what I want.

I want to read a file, if I found some specific string, I will insert some string to that location. Just like the function C language provided. Is there any functions in AutoIt that can do it?

Thanks.

$File = @DesktopDir & "\Something.txt"

$sStr = FileRead($File)

<do whatever you have to do to $sStr in here>

$oFile = FileOpen($File, 2)

FileWrite($oFile, $sStr)

FileClose($oFile)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

$File = @DesktopDir & "\Something.txt"

$sStr = FileRead($File)

<do whatever you have to do to $sStr in here>

$oFile = FileOpen($File, 2)

FileWrite($oFile, $sStr)

FileClose($oFile)

That's not what the OP is asking :)

I saw a post in the Example forum that would do this nicely the other day: http://www.autoitscript.com/forum/index.ph...4615&hl=hex

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.

Link to comment
Share on other sites

One of use has misunderstood the question. Now it remains to be seen which one.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

One of use has misunderstood the question. Now it remains to be seen which one.

Thank you guys.

The FileWrite or FileWriteline is not what I need. Because it inserts the string to the end of the file. What I want is to insert the sting INSIDE a file, basically insert the string where I read some special characters in the file.

I will read SmOke_N's suggestion and see if it helps.

Thanks again.

Link to comment
Share on other sites

Thank you guys.

The FileWrite or FileWriteline is not what I need. Because it inserts the string to the end of the file. What I want is to insert the sting INSIDE a file, basically insert the string where I read some special characters in the file.

I will read SmOke_N's suggestion and see if it helps.

Thanks again.

Did you really look at that code. The part where it says to read the file to a variable first. Then do any StringReplace, StringRegExpReplace, or other string manipulation. Then you have fileopen withe the mode set to 2 which will empty the files old contents. then write the corrected contents back in and close it

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Did you really look at that code. The part where it says to read the file to a variable first. Then do any StringReplace, StringRegExpReplace, or other string manipulation. Then you have fileopen withe the mode set to 2 which will empty the files old contents. then write the corrected contents back in and close it

Sorry, you are right. I did not read your code correctly. Your method should work. However, read everything into a single parameter and then find something will be a little bit difficult. I like to use FileReadLine to process each line. You know in C you can read the file and mark your current location.

I was wondering how come AutoIt does not have this feature.

Link to comment
Share on other sites

Sorry, you are right. I did not read your code correctly. Your method should work. However, read everything into a single parameter and then find something will be a little bit difficult. I like to use FileReadLine to process each line. You know in C you can read the file and mark your current location.

I was wondering how come AutoIt does not have this feature.

In that case look in the help file UDFs section for _FileReadToArray. Then just loop through the array and use the same method as I gave you.

BTW: AutoIt does have FileReadLine() but that (in any language) is the slowest possible method to process a file sinply because of the way it works. It's not bad on a short file but if it is a long file then reading it to an array is the best way to go.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

In that case look in the help file UDFs section for _FileReadToArray. Then just loop through the array and use the same method as I gave you.

BTW: AutoIt does have FileReadLine() but that (in any language) is the slowest possible method to process a file sinply because of the way it works. It's not bad on a short file but if it is a long file then reading it to an array is the best way to go.

Thanks. I think this is a good method. I have used a different method. I read each line from the file and then write it to another file. Therefore, I can insert any string I need during this reading/writing process. I will give your method a try when I get a chance.

Thanks again for your help.

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...