Jump to content

FileWrite question


LMTA
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

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

Link to comment
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 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

Glad to be of service :(

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

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