Jump to content
Sign in to follow this  
Docfxit

How to know a file was written successfully

Recommended Posts

Docfxit

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

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
Docfxit

 

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

Share this post


Link to post
Share on other sites
Docfxit

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

Share this post


Link to post
Share on other sites
water

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


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
Docfxit

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

Share this post


Link to post
Share on other sites
Docfxit

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

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
Docfxit

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

Share this post


Link to post
Share on other sites
water

Seems we have another satisfied customer here :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Docfxit

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

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  

×