red0fireus Posted July 30, 2020 Posted July 30, 2020 (edited) #include <File.au3> AdlibRegister("_readFile") While 1 Sleep(10) WEnd If _readFile() = 1 then ConsoleWrite("True!") Elseif _readFile() = 0 then ConsoleWrite("False!") EndIf Func _readFile() $sFilePath = @ScriptDir & "\ExeFiles.txt" FileOpen($sFilePath, 0) For $i = 1 to _FileCountLines($sFilePath) $line = FileReadLine($sFilePath, $i) If ProcessExists($line) then Return 1 EndIf Return 0 Next FileClose($sFilePath) EndFunc I have the above script and for some reason it will not return 1 or 0. If I replace "Return 1" with ConsoleWrite("True") it returns just fine. I'm not sure what I'm doing wrong. Edited July 30, 2020 by red0fireus
Danp2 Posted July 30, 2020 Posted July 30, 2020 Your If statement with the consolewrite won't occur because the script is stuck in the While loop. Also, your For...Next in _readFile doesn't make sense because your code always exits on the first iteration. Latest Webdriver UDF Release Webdriver Wiki FAQs
red0fireus Posted July 30, 2020 Author Posted July 30, 2020 #include <File.au3> AdlibRegister("_readFile") If _readFile() = 1 then ConsoleWrite("True!") Elseif _readFile() = 0 then ConsoleWrite("False!") EndIf While 1 Sleep(10) WEnd Func _readFile() $sFilePath = @ScriptDir & "\ExeFiles.txt" FileOpen($sFilePath, 0) For $i = 1 to _FileCountLines($sFilePath) $line = FileReadLine($sFilePath, $i) If ProcessExists($line) then return 1 If not ProcessExists($line) then return 0 endif EndIf Next FileClose($sFilePath) EndFunc Alright, I got it. Thank you
Danp2 Posted July 30, 2020 Posted July 30, 2020 FWIW, your multiple If statements within the For loop still don't make sense. If the first condition is met, then the function will return 1. The inner If statement will never be executed. Latest Webdriver UDF Release Webdriver Wiki FAQs
red0fireus Posted July 31, 2020 Author Posted July 31, 2020 (edited) I was able to execute the inner if statement. The inner if statement will only be executed if there are no exe files running stated in the exefiles.txt So my setup is I have a exefiles.txt file where each line has a different running or not running process --------------------------------------------- Exefiles.txt --------------------------------------------- test.exe (fake program) help.exe (fake program) rainmeter.exe (real program) (running) abc.exe (fake program) --------------------------------------------- End --------------------------------------------- With this setup the inner loop will not run and it will return true. If I were to remove the (real program) "rainmeter.exe" then my script will return false. I don't doubt that there was a better way of doing this, I'm trying to write a universal script for people to edit and I thought this would be the best option. The process must also be running for it to return true or 1 Edited July 31, 2020 by red0fireus
mikell Posted July 31, 2020 Posted July 31, 2020 4 hours ago, red0fireus said: With this setup the inner loop will not run and it will return true. If I were to remove the (real program) "rainmeter.exe" then my script will return false. Yes but this doesn't mean that the inner if statement was executed. As Danp2 said it can never be executed, so it is useless and could safely be removed The script returns false because it is the default return value : "user-defined functions return 0 unless another return value is specified" - ( helpfile )
Dan_555 Posted July 31, 2020 Posted July 31, 2020 (edited) @red0fireus Here is the explanation of what is happening. play with the $test variable and test it with -1, 0, 1 and 2 and watch the console output. global $test=-1 if Testme()=0 then CW("It is 0") elseif testme()=1 Then CW("it it 1") EndIf Func Testme() $test=$test+1 cw($test) Return $test EndFunc Func CW($txt) ConsoleWrite ($txt & @crlf) EndFunc It is better to do: local $tmp=_readFile() If $tmp = 1 then ConsoleWrite("True!") Elseif $tmp = 0 then ConsoleWrite("False!") EndIf Remember: Return statement leaves the function as soon as the condition is met. Therefore, each call of the _readFile() will return the exact same results. (because the $i loop starts from the same position) But by using the Return to get out of the function, the filehandle is not closed. (the FileClose($sFilePath) line ... ) You should take the loop, which counts the lines, out of the function. (if you want to have all of the executables (in the textfile) checked). Or place the found/not found results in an array. ... Edit: I have, somehow, overseen that you are calling the AdlibRegister("_readFile") this means that you plan to do some kind of loop check. Edited July 31, 2020 by Dan_555 Some of my script sourcecode
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now