Jump to content

Self-restarting script? :)


dv8
 Share

Recommended Posts

yups...like Suicide.au3 (a self-deleting script) you could make a batch file when the button is clicked which closes the script and then starts it again...

not sure if that will work, but just a suggestion

I believe we should all pay our tax with a smile. I tried - but they wanted cash!

Link to comment
Share on other sites

I have a script with "Restart" menuitem. I need when "Restart" is pressed, the script to restart itself (exit and then start again).

Would me more like: start executable and then exit.

Treat the compiled executable just like another exe file.

Link to comment
Share on other sites

Would me more like: start executable and then exit.

Treat the compiled executable just like another exe file.

Not happen that way, because there can be only one instance of it.

it has the line:

If UBound(ProcessList(@ScriptName)) > 2 Then Exit

That's because it has many HOTKEYS and if there is another instance the hotkeys mess up. :o

Link to comment
Share on other sites

Just create a script in the same directory that's something like this:

ProcessClose($firstScriptName)
Sleep(2000)
Run(@ComSpec & FileGetShortName(@ScriptDir) & "\" & $firstScriptName)

That should work just fine.

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Link to comment
Share on other sites

  • Moderators

That's not an option. As you can see the topic title says: Self-Rstarting script :o

Well, the idea that you have
If UBound(ProcessList(@ScriptName)) > 2 Then Exit
Is easily overcome, all the user has to do is rename the .exe ... so that's all you have to do too.

I don't know if this is all correct... but I tested it once and it seemed to work:

#include <GUIConstants.au3>
#include <file.au3>
Global $NULL = '', $fz_Name = ''
Global $PathSplit = _PathSplit(@AutoItExe, $NULL, $NULL, $fz_Name, $NULL)
If IsArray($PathSplit) Then
    If StringInStr($PathSplit[3], '___') Then Execute('Opt("OnExitFunc", "_SelfDelete")')
EndIf
Opt('MouseCoordMode', 2)
$Main = GUICreate("Example", 140, 60,-1, -1)
$Button1 = GUICtrlCreateButton("Restart", 20, 10, 100, 40)
GUISetState()
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
    If $msg = $Button1 Then 
        If IsArray($PathSplit) Then
            If Not StringInStr($PathSplit[3], '___') Then
                NewExe()
            ElseIf StringInStr($PathSplit[3], '___') Then 
                MsgBox(0, 'Error', 'Error, you must exit completely then manually restart')
            EndIf
        EndIf
    EndIf
WEnd

Func NewExe()
    FileCopy(@ScriptFullPath, @ScriptDir & '\' & StringTrimRight(@ScriptName, 4) & '____.exe')
    Sleep(1000)
    If FileExists(@ScriptDir & '\' & StringTrimRight(@ScriptName, 4) & '____.exe') Then Run(@ScriptDir & '\' & StringTrimRight(@ScriptName, 4) & '____.exe')
    Exit
EndFunc

Func _SelfDelete()
    If IsArray($PathSplit) Then
        If StringInStr($PathSplit[3], '___') Then
            Local $sCmdFile = ''
            FileDelete(@TempDir & "\scratch.bat")
            $sCmdFile = 'ping -n ' & '127.0.0.1 > nul' & @CRLF _
                    & ':loop' & @CRLF _
                    & 'del "' & @ScriptFullPath & '"' & @CRLF _
                    & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
                    & 'del ' & @TempDir & '\scratch.bat'
            FileWrite(@TempDir & "\scratch.bat", $sCmdFile)
            Run(@TempDir & "\scratch.bat", @TempDir, @SW_HIDE)
        EndIf
    EndIf
EndFunc
There's no error checking, so you would have to do that yourself... But I did make sure that it didn't delete the original copy, only the one that you are making to restart.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

That way I guess the idea about creating a temp batch file to do the dirty job :o would be useful.

Only trouble is I have no idea how to use batch files...search the forum for a Suicide.au3 and try deciphering that. That is a self-deleting script...you can always modify it to suit ur needs.

Edit: Yay...SmOke_N's method works on my box too...

Edited by kclteam

I believe we should all pay our tax with a smile. I tried - but they wanted cash!

Link to comment
Share on other sites

  • Moderators

That way I guess the idea about creating a temp batch file to do the dirty job :o would be useful.

Only trouble is I have no idea how to use batch files...search the forum for a Suicide.au3 and try deciphering that. That is a self-deleting script...you can always modify it to suit ur needs.

Edit: Yay...SmOke_N's method works on my box too...

If you type SelfDelete (Then Space Bar) in SciTe, it will automatically insert in your script :geek: .

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Mine doesnot enter any function...though it makes the term SelfDelete red in color...

SciTe version is 1.67...do I need to upgrade or something?

I believe we should all pay our tax with a smile. I tried - but they wanted cash!

Link to comment
Share on other sites

  • Moderators

Mine doesnot enter any function...though it makes the term SelfDelete red in color...

SciTe version is 1.67...do I need to upgrade or something?

Press the space bar right after you type it.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Mine doesnot enter any function...though it makes the term SelfDelete red in color...

SciTe version is 1.67...do I need to upgrade or something?

Tou may need to update your definitions as sometimes may not setup the abbreviations correctly on a new install. It can be done through Scite Config or the Start Menu.

@dv8

How about this for restarting.

; Requires Beta

If Not $CMDLINE[0] Then
    If UBound(ProcessList(@ScriptName)) > 2 Then Exit
ElseIf $CMDLINE[1] <> '/restart' Then
    Exit    
EndIf

;...
;...
;...

While 1
    Switch GUIGetMsg()
        Case $buttonGO
            Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /restart')
            Exit
;...
;...
;...
Edited by MHz
Link to comment
Share on other sites

  • Moderators

Tou may need to update your definitions as sometimes may not setup the abbreviations correctly on a new install. It can be done through Scite Config or the Start Menu.

@dv8

How about this for restarting.

; Requires Beta

If Not $CMDLINE[0] Then
    If UBound(ProcessList(@ScriptName)) > 2 Then Exit
ElseIf $CMDLINE[1] <> '/restart' Then
    Exit    
EndIf

;...
;...
;...

While 1
    Switch GUIGetMsg()
        Case $buttonGO
            Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /restart')
            Exit
;...
;...
;...
That's super nice MHz!!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I decided to compromise with:

If UBound(ProcessList(@ScriptName)) > 2 Then Exit

making it :

If UBound(ProcessList(@ScriptName)) > 3 Then Exit

And use this function to restart:

Func _Restart()
     Run(@ScriptFullPath)
     exit
EndFunc

:o

If anyone can come up with a way to self-restart script (by this i mean FIRST exit he script and THEN start it again) without any additional BAT files, please notify me :geek:

Link to comment
Share on other sites

  • Moderators

I decided to compromise with:

If UBound(ProcessList(@ScriptName)) > 2 Then Exit

making it :

If UBound(ProcessList(@ScriptName)) > 3 Then Exit

And use this function to restart:

Func _Restart()
     Run(@ScriptFullPath)
     exit
EndFunc

:o

If anyone can come up with a way to self-restart script (by this i mean FIRST exit he script and THEN start it again) without any additional BAT files, please notify me :geek:

MHz... has the perfect example... but you sure are picky!!... here's a working example
If Not $CMDLINE[0] Then
    If UBound(ProcessList(@ScriptName)) > 2 Then Exit
ElseIf $CMDLINE[1] <> '/restart' Then
    Exit    
EndIf

$Main = GUICreate("Example", 140, 60,-1, -1)
$Button1 = GUICtrlCreateButton("Restart", 20, 10, 100, 40)

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = - 3 Then Exit
    If $msg = $Button1 Then 
        Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /restart')
        Exit
    EndIf
WEnd

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Tou may need to update your definitions as sometimes may not setup the abbreviations correctly on a new install. It can be done through Scite Config or the Start Menu.

@dv8

How about this for restarting.

; Requires Beta

If Not $CMDLINE[0] Then
    If UBound(ProcessList(@ScriptName)) > 2 Then Exit
ElseIf $CMDLINE[1] <> '/restart' Then
    Exit    
EndIf

;...
;...
;...

While 1
    Switch GUIGetMsg()
        Case $buttonGO
            Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /restart')
            Exit
;...
;...
;...
I thought of this, but this way, when the script is started with "/restart" it won't have the UBOUND... line and it won't prevent starting another instance of the script... :)
Link to comment
Share on other sites

  • Moderators

I thought of this, but this way, when the script is started with "/restart" it won't have the UBOUND... line and it won't prevent starting another instance of the script... :)

Errr, yes it does... Just compile my example and see for yourself.

Edit: I should have said, look at the ElseIf Statement... I'm tired off to bed [Yawn] :):o:mellow: [/Yawn]

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

MHz... has the perfect example... but you sure are picky!!... here's a working example

Thanks.

I suppose one could use just

Run(@ScriptFullPath)

in the example without using Beta, but prevents Au3 execution as when UnCompiled. :)

@dv8

It is If conditional on whether a 2nd instance starts or not. Using the switch restart will allow a 2nd instance and close the first. You cannot have it both ways of executing a 2nd instance and not allowing it.

Edit:

If you want, have a look at FAQ 14 and apply it within the restart part of the If block.

Edited by MHz
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...