Jump to content

Help ID proper "Exit" command in script


 Share

Recommended Posts

I want the script to exit, after the second message box command (which is invoked if there is no network connection), whether or not  the user presses the OK button before the 10 second message box time out. Right now, the script continues, sequentially, instead of going to "Exit" at the end.

 

#RequireAdmin
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
$RemDir = "\\Network\HomeDir"
$process = "Outlook.exe"
$LogFile = FileOpen("M:\NAS\Log\SYNC2NAS\Sync.log", 1)
  Local $var = Ping("XX.XX.XX.90", 4000)
If $var Then
    ;double line message box
    MsgBox(64, "Backup", "CONNECTION EXISTS. . ." & @LF & "Starting backup to NAS in 4 seconds..." & @LF & @LF &$RemDir, 4)
Else
    MsgBox(0, "Network status", "Unable to connect..", 10)
EndIf
If ProcessExists($process) Then
    ProcessClose($process)
    sleep(1000)
    RUNWAIT ("RunCopyProgram.exe")
Else
    RUNWAIT ("RunCopyProgram.exe")
FileWriteLine($LogFile, "Selected program directories successfully backed up on: >>>  " & @MDAY & "/" & @MON & " " & @HOUR & ":" & @MIN & @CRLF)
FileClose($LogFile)
 MsgBox(48, "Sync", "Selected program directories have been backed up" & @LF & "This box will close in three seconds . .", 3)
EndIf
Exit

 

Link to comment
Share on other sites

If I do that, the script exits regardless of whether there is a network connection or not. I only want to exit if there is not a network connection.

Sorry didn't follow your instructions fully. I put it right under message box line as you said. It now works. Thanks a lot.

Edited by copyleft
Link to comment
Share on other sites

If $var Then
    ;double line message box
    MsgBox(64, "Backup", "CONNECTION EXISTS. . ." & @LF & "Starting backup to NAS in 4 seconds..." & @LF & @LF &$RemDir, 4)
Else
    MsgBox(0, "Network status", "Unable to connect..", 10)
    FileClose($LogFile)
    Exit
EndIf

 

Edited by pseakins
added file close

Phil Seakins

Link to comment
Share on other sites

Just a shorter version:

#RequireAdmin
$process = "Outlook.exe"
If Not Ping("XX.XX.XX.90", 4000) Then Exit MsgBox(0, "Network status", "Unable to connect..", 10)
MsgBox(64, "Backup", "CONNECTION EXISTS. . ." & @LF & "Starting backup to NAS in 4 seconds..." & @LF & @LF & "\\Network\HomeDir", 4)
If ProcessExists($process) Then Sleep(1000 * ProcessClose($process))
RunWait("RunCopyProgram.exe")
$LogFile = FileOpen("M:\NAS\Log\SYNC2NAS\Sync.log", 1)
FileWriteLine($LogFile, "Selected program directories successfully backed up on: >>>  " & @MDAY & "/" & @MON & " " & @HOUR & ":" & @MIN & @CRLF)
FileClose($LogFile)
MsgBox(48, "Sync", "Selected program directories have been backed up" & @LF & "This box will close in three seconds . .", 3)

 

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

14 minutes ago, copyleft said:

What's the function of the asterisk after sleep in line 5?

if ProcessExists($process) Then Sleep(1000 * ProcessClose($process))

This is just another notation for :

If ProcessExists($process) Then
    ProcessClose($process)
    sleep(1000)

ProcessClose returns 1 (=Success) or 0 (=Failure). The Sleep therefore has a duration of 1000 times 1 or 1000 times 0 milliseconds.

==> there is only a Sleep if the Process exists.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

6 hours ago, Musashi said:

==> there is only a Sleep if the Process exists.

What if the process exists but the ProcessClose() fails? With @exit's code there is only a sleep if the process exists and it is successfully closed. Plus, statements like this are cool.

Phil Seakins

Link to comment
Share on other sites

@pseakins You could use bitwise operations perhaps?

BitOr(ProcessExists($process),ProcessClose($process))

BitAnd(ProcessExists($process),ProcessClose($process))

Idk, which one (or some other operation) would return what you want though as I have not followed the thread.

Link to comment
Share on other sites

1 hour ago, pseakins said:

What if the process exists but the ProcessClose() fails? With @exit's code there is only a sleep if the process exists and it is successfully closed. Plus, statements like this are cool.

Might as well skip the if altogether and just say:

Sleep(1000 * ProcessClose($process))

 

Code hard, but don’t hard code...

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