Jump to content

cannot open file with write access but can create file


Recommended Posts

Hello all. I have the following lines in a script to save data to a text file. If the file does not exist, the file is created successfully. Also, if the file exists, another function using the same FileOpen but with "0" mode for read only successfully retrieves the data from the file.

However, if the file already exists, and the overwrite mode is specified, it errors out. In desperation, I also tried using FileDelete first to remove it and then create it again and this also receives an error.

$file_msg = FileOpen($bfile, 2)
If $file_msg = -1 Then
    MsgBox(16, "Error 106", "We could not save the file to this location.")
    Return 0
Else
    $write = FileWrite($file_msg, $data_to_write)
    If $write = 0 Then
        MsgBox(16, "Error 107", "We could not save the data to the file selected.")
    EndIf
            
    ; close the file
    FileClose($file_msg)
EndIf

I have also tried using the WinAPI_CreateFile, etc and got the same error. Also moved the file being overwritten to the shared docs on the computer and it still wouldn't overwrite. Im on XP SP3 and cannot for the life of me figure this one out!

Again, keep in mind that when the same code is run and the file doesn't exist yet, it is created with the data specified above.

Thanks!

Aaron

Link to comment
Share on other sites

There must be something wrong with your filename or elsewhere, since the following works in all cases (file already exists or not):

Local $file_msg = FileOpen(@ScriptDir & '\test.txt', 2)
If $file_msg = -1 Then
    MsgBox(16, "Error 106", "We could not save the file to this location.")
    Return 0
Else
    $write = FileWrite($file_msg, 'this is data')
    If $write = 0 Then
    MsgBox(16, "Error 107", "We could not save the data to the file selected.")
    EndIf

    ; close the file
    FileClose($file_msg)
EndIf

Check if your filename contains a directory and if so, check if flag 8 (create directory) isn't needed in addition to 2 (overwrite mode).

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)

Link to comment
Share on other sites

There must be something wrong with your filename or elsewhere, since the following works in all cases (file already exists or not):

Thanks for the input. I have checked the variable over and over and am certain it is correct. In verifying this, I think I have an idea of what it is although not certain specifically.

This behavior only happens when I have only a few lines earlier opened (and closed) the file with the same name and read the contents. When doing this, then the overwrite errors. If I alternatively open and read one document and then overwrite a different doc, it works.

So, is there some sort of time constraint or something that needs to be done after reading a file so that it will be able to overwrite?

-Aaron

Link to comment
Share on other sites

So, is there some sort of time constraint or something that needs to be done after reading a file so that it will be able to overwrite?

Found the problem, I knew it had to be something stupid. There was a mistyped variable in the previous function that opened the file and read it so it was NOT in fact closing it correctly.

Thanks!

Link to comment
Share on other sites

No problem, we all commit horrible blemishes. I'd rather have one more in my (or your) counter than discover a fatal, difficult to workaround flaw in a tool on which I rely for mission business!

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)

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