Sign in to follow this  
Followers 0
samueltb

setting error level on completetion of script

7 posts in this topic

Hi I'm currently attempting to write a script which checks the time on a server, if the time is prior to 7:45 I want to force the script to exit and set errorlevel which I'll be able to pick up externally of the script using altiris. the script at present will create a file which I can then do a dir command and if exist continue, it would be easier and cleaner if I could sipmly set the error level and pick that up, hope this makes sense.

#include <Date.au3>

FileDelete ("c:\temp\time.txt")

$var1 = _NowTime ()

if $var1 < "07:45:00" Then

FileOpen ("c:\temp\time.txt", 1)

FileWrite ("c:\temp\time.txt", $var1)

FileClose ("c:\temp\time.txt")

endif

Thanks for any help

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You are using the File functions wrong.

$File = FileOpen("C:\temp\time.txt", 2)
FileWrite($File,$var1)
FileClose($File)

Edit: Try to see if you can capture the 'Exit' value from the program. Then use Exit 1 and Exit 0 to give the difference.

Edited by Manadar

Share this post


Link to post
Share on other sites

Cheers for the help, but I'm none the wiser, the exit value is always 0 as the script always executes.

Share this post


Link to post
Share on other sites

You could start by reading the help file.

You might check out the Exit command in the help file.

This might help (it's from the help file):

Exit

--------------------------------------------------------------------------------

Terminates the script.

Exit [return code]

Parameters

return code [optional] Integer that sets the script's return code. This code can be used by Windows or the DOS variable %ERRORLEVEL%. The default is 0. Scripts normally set an errorlevel of 0 if the script executed properly; error levels 1 and above typically indicate that the script did not execute properly.

It would probably help to spend more time with the help file, too.

Do we see a pattern here?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I have tried the help file, I modified my script to display the exit code but in dos %errorlevel% is reporting back as 0 regardless of what EXIT is set to.

#include <Date.au3>

Opt("OnExitFunc", "endscript")

Func endscript()

MsgBox(0,"","after last statement " & @EXITCODE)

EndFunc

FileDelete ("c:\temp\time.txt")

DirCreate ("c:\temp")

$var1 = _NowTime ()

if $var1 < "07:00:00" Then

$file = FileOpen ("c:\temp\time.txt", 1)

FileWrite ("c:\temp\time.txt", $var1)

FileClose ("c:\temp\time.txt")

Exit (1)

EndIf

exit (0)

Share this post


Link to post
Share on other sites

Using < or > with strings may not give you the results you want.

This may work before 7am with some luck :)

#include <Date.au3>
Opt("OnExitFunc", "endscript")

Func endscript()
    MsgBox(0, "", "after last statement " & @exitCode)
EndFunc

FileDelete("c:\temp\time.txt")
DirCreate("c:\temp")
$var1 = _NowTime(4)

If Int(StringLeft($var1, 2)) < 7 Then
    $handle = FileOpen("c:\temp\time.txt", 1)
    If $handle <> -1 Then
        FileWrite($handle, $var1)
        FileClose($handle)
    EndIf
    Exit 1
EndIf

Exit

Use the file handles also.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Maybe simpler:

#include <File.au3>

FileDelete("c:\temp\time.txt")
If (@HOUR < 7) Or (@HOUR = 7 And @MIN < 45) Then
    _FileWriteLog("c:\temp\time.txt", "Tried to start script before 07:45.")
    Exit 1
Else
    Exit 0
EndIf

Note, _FileWriteLog() adds a date/time stamp automatically, which for me is cleaner and easier.

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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