LMTA 0 Posted April 13, 2010 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) EndFuncAs 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
water 2,391 Posted April 13, 2010 (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 April 13, 2010 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - WikiTask Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - WikiTutorials:ADO - Wiki, WebDriver - Wiki Share this post Link to post Share on other sites
LMTA 0 Posted April 13, 2010 @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
water 2,391 Posted April 13, 2010 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 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - WikiTask Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - WikiTutorials:ADO - Wiki, WebDriver - Wiki Share this post Link to post Share on other sites
LMTA 0 Posted April 13, 2010 (edited) That's clear, thank you very much for your explanation! Edited April 13, 2010 by LMTA Share this post Link to post Share on other sites
water 2,391 Posted April 13, 2010 Glad to be of service My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - WikiTask Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - WikiTutorials:ADO - Wiki, WebDriver - Wiki Share this post Link to post Share on other sites