Jump to content

Simple Script not Working: "Return 1" and "Return 0"


Recommended Posts

#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 by red0fireus
Link to comment
Share on other sites

#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

Link to comment
Share on other sites

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 by red0fireus
Link to comment
Share on other sites

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 )

Link to comment
Share on other sites

@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 by Dan_555

Some of my script sourcecode

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