oasis375

FileOpen / FileWrite Bug?

7 posts in this topic

This doesn't work, it deletes (overwrites) file contents:

$hFile = FileOpen("file.txt", 2)
$content1 = FileRead($hFile)
$content2 = StringReplace($content1, "old", "new")
FileWrite($hFile, $content2)
FileClose($hFile)

Why? Instead, I have to use this workaround:

$hFile = FileOpen("file.txt")
$content1 = FileRead($hFile)
$content2 = StringReplace($content1, "old", "new")
FileClose($hFile)
$hFile = FileOpen("file.txt",2)
FileWrite($hFile, $content2)
FileClose($hFile)

That's it, opening, closing and reopening the file.

Function reference says: FileWrite. "Write text/data to the end of a previously opened file." (Does it mean "append"?).

Remarks: "The file must be opened in write mode or the FileWrite() command will fail."

But what write mode? $FO_APPEND (1) = Write mode (append to end of file). $FO_OVERWRITE (2) = Write mode (erase previous contents).

 

Share this post


Link to post
Share on other sites



In the general case, changing part of a file implies reading it, performing the change then rewriting it in full.

Yet you can somehow streamline the code:

$changed = StringReplace(FileRead("file.txt"), "old", "new")
$hFile = FileOpen("file.txt", 2)
FileWrite($hFile, $changed)
FileClose($hFile)

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

OK, finally I discovered that AutoIt doesn't have a read&write mode, like other programming languages, but there's a workaround using FileSetPos() before each Read/Write operation.

$hFile = FileOpen("file.txt", 1)
FileSetPos($hFile, 0, 0)
$content1 = FileRead($hFile)
$content2 = StringReplace($content1, "old", "new")
FileSetPos($hFile, 0, 0)
FileWrite($hFile, $content2)
FileClose($hFile)

Reference:

FileOpen("filename", $FO_OVERWRITE) deletes file contents, at the same moment.

Share this post


Link to post
Share on other sites

The simplest way, in AutoIt, to do what you are doing is this

#include <FileConstants.au3>

$content1 = FileRead("file.txt")
$hFile = FileOpen("file.txt", $FO_OVERWRITE)
$content2 = StringReplace($content1, "old", "new")
FileWrite($hFile, $content2)
FileClose($hFile)

 

1 person likes this

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Isn't that what I posted above?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
1 minute ago, jchd said:

Isn't that what I posted above?

Yes it is but oasis375 didn't appear to understood your your first compounded line, so I set it out one statement per line to make it clearer.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
8 hours ago, jchd said:

Isn't that what I posted above?

Sorry if I upset you. What I didn't understand was that AutoIt doesn't have a method to read and write at the same time, and that $FO_OVERWRITE deletes file contents, at the same moment. So mine was a conceptual problem, not a coding one.

Anyway, thank you guys for your replies.

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

  • Similar Content

    • Jibberish
      By Jibberish
      Junior Programmer here... 
      Not much experience with opening, changing and closing files.
      I am trying to replace strings in a Text file except StringReplace does not actually replace the text.
      Here is a sample of my code...
      #include <File.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> Local $iStrReturn = 0 Local $hFile Local $sText Local $sNewText ; Location of File to be read $sFileName = "C:\Temp\MyPlayer.exe.config" ; The default is FALSE. We want to change this to TRUE $bLoopChecked = True CheckBox() Func Checkbox() $hFile = FileOpen($sFileName,$FO_READ) ; Open file in read mode to get text If $hFile = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when Opening the file.") Exit EndIf FileSetPos($hFile, 0, 0) ; No idea if I need to do this, grasping at straws $sText = FileRead($hFile) ; Read the file into $sText If $sText = 1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading/writing the file.") Exit Else FileClose($hFile) ; Finished reading the file into $sText, so close the file. FileFlush($hFile) ; Manual says to use FileFlush between File Close and Open so here it is EndIf MsgBox(0,"Before Replacement",$sText) ; Displays the text read from the file to make sure something is there. ; Loop Check If $bLoopChecked = True Then ; Find the string return > 0 for success $iStrReturn = StringInStr('"<add key="LoopCheckbox" value=""False" />"', "False") ;MsgBox(0,"", "LoopCheckBox is " & $iStrReturn) If $iStrReturn > 0 Then ; If StringInStr returned > 0 the it found the string! ; The Meat of the code. This is where we have to replace "False" with "True" $sNewText = StringReplace($sText, '"<add key="LoopCheckbox" value="False" />"', '"<add key="LoopCheckbox" value="True" />"') MsgBox(0,"After Replacement",$sNewText) ; Display the text to see if it worked. $hFile = FileOpen($sFileName,$FO_OVERWRITE) ; Reopen the file to write to it, overwriting everything. FileWrite($hFile,$sNewText) ; Write the text to the file FileClose($hFile) ; Close the file EndIf EndIf EndFunc This is the file I am reading...
      <?xml version="1.0" encoding="utf-8"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/> </startup> <appSettings> <add key="LoopCheckbox" value="false"/> </appSettings> </configuration> I tried opening the file with $FO_UTF8 and $FO_UTF8_NOBOM but got errors opening the file.
      The MsgBox "After Replacement" shows the value is still false.
    • wimhek
      By wimhek
      Is it possible , and how can I read and write txt files from Icloud (apple service) ?
      Let me try to explain my application.
      On my Ipad and Iphone I create txt files. On my windows computer it is possible to read and modify these files manually, by logging in on www.icloud.com.
      What I want to make is an auto-it script who reads the txt file and create an new txt file on www.icloud.com,  so I can acces these on my ipad and/or phone.
       
      Thank you.
    • num1g8rfan
      By num1g8rfan
      Hi All,
      I have a script whose sole purpose is to walk through about 15,000 - 20,000 files in a single directory structure, making modifications to 5 or 6 lines within each file.  Simple.  I have a small splash screen that is simply counting how many files have been completed out of the overall total.  The script is functioning fine and the splash screams through the initial files, but through time begins to slow to a crawl (It becomes noticeable after about 1,000 to 1,500 files).  
      I have used a combination of methods to write to the file, but none of which appear to make much of any difference:
      Local $FileOpen = FileOpen($FileInList,1) FileWriteLine($FileOpen,@CRLF) FileClose($FileOpen) OR
      FileWriteLine($FileHandle, $NewLineString & @CRLF) OR
      Local $FileHandle = FileOpen($FileInList,2) _FileWriteFromArray($FileHandle,$FileContentArray,1) FileClose($FileHandle) I have checked and the CPU and memory are fine.  My current work-around is to create 5-10 separate directory structures from the original one that contains 20,000 files, run the script and then combine them again in the end.  It works, but isn't very portable.  Any ideas?
      Cleanup_AutoITPost.au3
    • MrSparty1995
      By MrSparty1995
      I have a good handle on how to read a file line by line, and search for a given string.
      Basically using, FileOpen, FileReadLine, and StringInStr.
      I have been trying to figure out why my search keeps coming back with no match.
      For example, if my string line was "Where is Waldy", and I was searching for Waldy that comes back fine.
      But in my file, Let's say it is
      Where is "Waldy"
      So i read that line in and do a search for Waldy, but i comes back with no match since there is quotes around it.
      Is there a way to resolve this?
      Here is my actual example
      The line comes back like this
          <JposEntry logicalName="LineDisp_iSC480">
      I'm searching for LineDisp_iSC480, but its coming back with no match due to the quotes.
      Thanks in advance, James
    • fmlk
      By fmlk
      Hello,
      In a main script I have added logging and that is working. Now I created another small script where I need also logging. So I used the same code:
      What is wrong? Why is the logfile not created?

      $default_delay = 2000
      $logFilePath = ("C:\AutoIT_TestLogs")
      DirCreate($logFilePath)
      $Logfile = FileOpen($logFilePath & "\GMS_install" & @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & ".log",1)
      FileWriteLine($logFile,@CR & @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & "_Install_licenses")
      Consolewrite("Install_licenses" & @CR)
      Run("C:\Users\factory\Desktop\LicenseInstaller\LicenseInstaller.exe","C:\Users\factory\Desktop\LicenseInstaller")
      Sleep(1000)
      WinWait("Gatan License Installer")
      WinActivate("Gatan License Installer")
      WinWaitActive("Gatan License Installer")
      ControlClick("[ACTIVE]","","[NAME:CMD_Install]")
      Sleep($default_delay)
       If WinExists("LicenseInstaller") Then
           WinActivate("LicenseInstaller")
            WinWaitActive("LicenseInstaller")
            ControlClick("[ACTIVE]","","[CLASS:Button; INSTANCE:1]")
              sleep($default_delay)
         EndIf
      FileWriteLine($logFile,@CR & @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & "_Licenses_Installed")
      Consolewrite("Licenses are installed" & @CR)