Jump to content

How to know a file was written successfully


Recommended Posts

I am trying to write to the hosts file.  If the hosts file is read only this script works great.  If the hosts file is not read only it gets written to twice. 

I think I have two problems.

1.  When the first filewrite happens the script doesn't recognize it was written to successfully.

2.  On the second filewrite the file needs to be open before the write.  I don't understand why the file isn't open already.  Why does it need to be open again?

$strHost = @SystemDir & "\drivers\etc\hosts"
            $tcpIP = "192.168.254.254"
            $sWhat = "AnyDomain"
           
                       
$hFile = FileOpen($strHost, 1) 
            FileWrite($hFile, @LF & $tcpIP & " " & $sWhat)
            If @error = 1 Then
                MsgBox($MB_TOPMOST, 'TestBox 3', "Host file was written successfully  " & "@error = " & @error & @CRLF & $strHost)
            Else
                If Not FileSetAttrib($strHost, "-R") Then
                    MsgBox($MB_TOPMOST, 'TestBox 3', "After Host file write was read only " & "@error = " & @error & @CRLF & $strHost)
                EndIf
                $hFile = FileOpen($strHost, 1)  ;  I don't understand why the file needs to be open again.  It wasn't closed
                FileWrite($hFile, @LF & $tcpIP & " " & $sWhat)
                FileSetAttrib($strHost, "+R")
            EndIf
            FileClose($hFile)

Thank you,

Docfxit

Edited by Docfxit
Link to comment
Share on other sites

You're checking @error to see if it's 1 or not, you should be checking whether FileWrite returns 1 or not, not @error. So if it writes to the file successfully, @error won't equal 1, so it writes it again.

FileWrite($hFile, @LF & $tcpIP & " " & $sWhat)
If @error = 1 Then
    MsgBox(0x40000, 'TestBox 3', "Host file was written successfully  " & "@error = " & @error & @CRLF & $strHost)
Else

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

1/ You are confusing @error and return value.

2/ Can you really change a file attribute (especially RO) with that file currently open for write?

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

 

You're checking @error to see if it's 1 or not, you should be checking whether FileWrite returns 1 or not, not @error. So if it writes to the file successfully, @error won't equal 1, so it writes it again.

FileWrite($hFile, @LF & $tcpIP & " " & $sWhat)
If @error = 1 Then
    MsgBox($MB_TOPMOST, 'TestBox 3', "Host file was written successfully  " & "@error = " & @error & @CRLF & $strHost)
Else

 

I changed the code to:

$filewritten = FileWrite($hFile, @LF & $tcpIP & " " & $sWhat)
            If $filewritten = 1 Then
                MsgBox($MB_TOPMOST, 'TestBox 3', "Host file was written  " & "@error = " & @error & @CRLF & $strHost)
            Else

And it works great now.  Thank you very much for pointing me in the right direction.

Docfxit

Edited by Docfxit
Link to comment
Share on other sites

1/ You are confusing @error and return value.

2/ Can you really change a file attribute (especially RO) with that file currently open for write?

 

1.  Thanks for letting me know.  I wish there was an example in the help file to show that.

2.  It seems to work.  I can certainly move the file attribute after the close.

Thank you very much,

Docfxit

Link to comment
Share on other sites

Magic Numbers in your MsgBox statement? If Guinness sees this, then ... ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

2/ Remember your own question: Why does it need to be open again?

My answer was that changing the file attibute forcibly closes the file, else the OS would be self-contradictory (writing to a RO file would work). Hence the need to reopen after that.

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

2/ Remember your own question: Why does it need to be open again?

My answer was that changing the file attibute forcibly closes the file, else the OS would be self-contradictory (writing to a RO file would work). Hence the need to reopen after that.

 

Oh Great.  Thank you for clarifying that.

Thank you,

Docfxit

Link to comment
Share on other sites

Magic Numbers in your MsgBox statement? If Guinness sees this, then ... ;)

 

It sounds like he might be upset? 

What are Magic Numbers?

Why shouldn't they be used?

I'd like to fix it before he sees it.  I'd just like to know what I am fixing and why.  I don't want anyone to get upset.

Thanks for the heads up.

Docfxit

Link to comment
Share on other sites

Just a joke ;)

But it is preferred to use the Constant Names as described in the help file for MsgBox.

Magic Numbers are descriibed as "Unique values with unexplained meaning ...". Using Constants will help you to be able to read your code in the future more easily.

Just replace 0x40000 with $MB_TOPMOST.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Docfxit,

water is right: don't do that.

guinness has a record of launching ICBMs against people who persist using magic numbers, so if your IP can be located close enough to your door you're putting your familly (and neighbours) at high risk.

Fortunately my ISP provide me with IPs that can only be located at either 250 or 430 km from where I actually live depending on how you use traceroute tools, so I'm pretty safe for now. I feel sorry for many people wounded or killed nearby the big hubs in my network...

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

Just a joke ;)

But it is preferred to use the Constant Names as described in the help file for MsgBox.

Magic Numbers are descriibed as "Unique values with unexplained meaning ...". Using Constants will help you to be able to read your code in the future more easily.

Just replace 0x40000 with $MB_TOPMOST.

 

Thank you for letting me know.

I have replaced what I could in this thread and all my scripts.

I'm always happy to learn better ways of doing things.

It always amazes me of how many ways there are of doing things.

And how many ways a person can get in trouble.

All joking aside ;)

Thanks,

Docfxit

Link to comment
Share on other sites

Seems we have another satisfied customer here :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Docfxit,

water is right: don't do that.

guinness has a record of launching ICBMs against people who persist using magic numbers, so if your IP can be located close enough to your door you're putting your familly (and neighbours) at high risk.

Fortunately my ISP provide me with IPs that can only be located at either 250 or 430 km from where I actually live depending on how you use traceroute tools, so I'm pretty safe for now. I feel sorry for many people wounded or killed nearby the big hubs in my network...

 

Amazon is currently testing a new delivery system with drones that look like helicopters. They deliver packages to your front door step.

What's your address?

Docfxit

PS:  I hope you got a laugh out of this.

Edited by Docfxit
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...