Sign in to follow this  
Followers 0
LMTA

FileWrite question

6 posts in this topic

Hi there AutoIT-guru's!

I see some behaviour with my script which I cannot explain.

First, let me explain the intention if this script:

I am trying to script a routine which will create a textfile in the directory where the script itself resides. This textfile should contain two values: the computername from the machine where the script has been started and also the username from the user who started it. When the file is created, the script start and does its thing. When another user or the same user tries to start the script as well or again, an error message should be displayed that the script can only be running once, together with the computer- and username read from the textfile so people are informed about who has been running the script already. This also explains why _Singleton does not serve the purpose.

So far, I came with these routines:

#Include <File.au3>

;ReadFile()
MakeFile()

Func ReadFile()
    $File = FileOpen("test.txt", 0)

    If $File = -1 Then
        MsgBox(0, "Error", "Can't open textfile!")
        Exit
    EndIf

    While 1
        $User = FileReadLine($File, 1)
        If @error = -1 Then ExitLoop
        $Station = FileReadLine($File, 2)
        MsgBox(48, "Already started", "The reader is already started by " _
            & $User & @LF & "on the PC " & $Station & "." & @LF _
            & "Please close this instance first before you start a new one.")
        If @error = 1 Then ExitLoop
    Wend
    FileClose($File)
EndFunc

Func MakeFile()
    $FileTest = _FileCreate("InUse.txt")
    FileOpen ($FileTest, 2)
    FileWrite ($FileTest, @UserName & @CRLF)
    FileWrite ($FileTest, @ComputerName)
    FileClose ($FileTest)
EndFunc

As you can see, there are two routines involved. There are currenty two 'problems' with my code:

  • Routine ReadFile() is looping indefinitely;
  • Routine MakeFile() is creating to file instead of one. One file is called '1' and the other (created first) is called "InUse.txt". The macro's @Username and @Computername are written into "1" and not in "InUse.txt".
Anyone any clues what mistakes I am making here? Any help would be appreciated!

Regards,

Sander.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Problem 1: You check the @error of MsgBox not of FileReadLine. Put @error into a variable and then check this variable.

$Station = FileReadLine($File, 2)
    $iResult = @error
    MsgBox(48, "Already started", "The reader is already started by " _
    & $User & @LF & "on the PC " & $Station & "." & @LF _
    & "Please close this instance first before you start a new one.")
    If $iResult = 1 Then ExitLoop

Problem 2: FileCreate does not return a handle of a file but a returncode of 1 or 0. You have to use a filename for FileOpen and then you can use the handle returned by FileOpen to write to the file.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@Water, thank you very much for your helpful suggestions.

Both problems are solved with your tips!

I noticed that FileReadLinegives an @error that equals 0.

Is this normal behaviour? In the help I only read these returncodes:

Success: Returns a line of text.

Special: Sets @error to -1 if end-of-file is reached.

Failure: Sets @error to 1 if file not opened in read mode or other error.

Share this post


Link to post
Share on other sites

This is normal behaviour.

On success AutoIt functions return the specified result and set @error to 0 (exceptions are documented in the help file).

On error @error and - if available - @extended are set to let you debug your code.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

That's clear, thank you very much for your explanation! :( Edited by LMTA

Share this post


Link to post
Share on other sites

Glad to be of service :(


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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  
Followers 0