{"action":"opened","issue":{"url":"https://api.github.com/repos/intellnet/UDF-SNIPPETS/issues/1","labels_url":"https://api.github.com/repos/intellnet/UDF-SNIPPETS/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/intellnet/UDF-SNIPPETS/issues/1/comments","events_url":"https://api.github.com/repos/intellnet/UDF-SNIPPETS/issues/1/events","html_url":"https://github.com/intellnet/UDF-SNIPPETS/issues/1","id":59418854,"number":1,"title":"_SelfRename","user":{"login":"intellnet","id":11240919,"avatar_url":"https://avatars.githubusercontent.com/u/11240919?v=3","gravatar_id":"","url":"https://api.github.com/users/intellnet","html_url":"https://github.com/intellnet","followers_url":"https://api.github.com/users/intellnet/followers","following_url":"https://api.github.com/users/intellnet/following{/other_user}","gists_url":"https://api.github.com/users/intellnet/gists{/gist_id}","starred_url":"https://api.github.com/users/intellnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/intellnet/subscriptions","organizations_url":"https://api.github.com/users/intellnet/orgs","repos_url":"https://api.github.com/users/intellnet/repos","events_url":"https://api.github.com/users/intellnet/events{/privacy}","received_events_url":"https://api.github.com/users/intellnet/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-03-01T21:37:51Z","updated_at":"2015-03-01T21:37:51Z","closed_at":null,"body":"#include-once\r\n#include <FileConstants.au3>\r\n#include <StringConstants.au3>\r\n\r\n; #FUNCTION# ====================================================================================================================\r\n; Name ..........: __SelfRename\r\n; Description ...: Rename the running executable to a different filename.\r\n; Syntax ........: _SelfRename($sFileName[, $fRestart = Default[, $iDelay = 5[, $fUsePID = Default]]])\r\n; Parameters ....: $sFileName           - Filename the executable should be called e.g. Example.exe or Example\r\n;                  $fRestart            - [optional] Restart the application (True) or to not restart (False) after overwriting. Default is False.\r\n;                  $iDelay              - [optional] An integer value for the delay to wait (in seconds) before stopping the process and deleting the executable.\r\n;                                         If 0 is specified then the batch will wait indefinitely until the process no longer exits. Default is 5 (seconds).\r\n;                  $fUsePID             - [optional] Use the process name (False) or PID (True). Default is False.\r\n; Return values .: Success - Returns the PID of the batch file.\r\n;                  Failure - Returns 0 & sets @error to non-zero\r\n; Author ........: guinness\r\n; Example .......: Yes\r\n; ===============================================================================================================================\r\nFunc _SelfRename($sFileName, $fRestart = Default, $iDelay = 5, $fUsePID = Default)\r\n    If @Compiled = 0 Then\r\n        Return SetError(1, 0, 0)\r\n    EndIf\r\n\r\n    Local Const $iPosition = StringInStr($sFileName, '.', $STR_NOCASESENSEBASIC, -1) - 1\r\n    If $iPosition Then\r\n        $sFileName = StringLeft($sFileName, $iPosition)\r\n    EndIf\r\n    Local $sAppID = @ScriptName\r\n    If $sFileName & '.exe' = $sAppID Then\r\n        Return SetError(3, 0, 0)\r\n    EndIf\r\n    $sFileName = @ScriptDir & '\\' & $sFileName & '.exe'\r\n\r\n    Local $sTempFileName = @ScriptName\r\n    $sTempFileName = StringLeft($sTempFileName, StringInStr($sTempFileName, '.', $STR_NOCASESENSEBASIC, -1) - 1)\r\n    While FileExists(@TempDir & '\\' & $sTempFileName & '.bat')\r\n        $sTempFileName &= Chr(Random(65, 122, 1))\r\n    WEnd\r\n    $sTempFileName = @TempDir & '\\' & $sTempFileName & '.bat'\r\n\r\n    If $iDelay = Default Then\r\n        $iDelay = 5\r\n    EndIf\r\n\r\n    Local $sDelay = ''\r\n    $iDelay = Int($iDelay)\r\n    If $iDelay > 0 Then\r\n        $sDelay = 'IF %TIMER% GTR ' & $iDelay & ' GOTO DELETE'\r\n    EndIf\r\n\r\n    Local $sImageName = 'IMAGENAME'\r\n    If $fUsePID Then\r\n        $sAppID = @AutoItPID\r\n        $sImageName = 'PID'\r\n    EndIf\r\n\r\n    Local $sCmdLineRaw = ''\r\n    If $CmdLineRaw <> '' Then\r\n        $sCmdLineRaw = ' ' & $CmdLineRaw\r\n    EndIf\r\n\r\n    Local $sRestart = ''\r\n    If $fRestart Or $fRestart = Default Then\r\n        $sRestart = 'START \"\" \"' & $sFileName & '\"' & $sCmdLineRaw\r\n    EndIf\r\n    $sCmdLineRaw = ''\r\n\r\n    Local Const $iInternalDelay = 2, _\r\n            $sScriptPath = @ScriptFullPath\r\n    Local Const $sData = 'SET TIMER=0' & @CRLF _\r\n             & ':START' & @CRLF _\r\n             & 'PING -n ' & $iInternalDelay & ' 127.0.0.1 > nul' & @CRLF _\r\n             & $sDelay & @CRLF _\r\n             & 'SET /A TIMER+=1' & @CRLF _\r\n             & @CRLF _\r\n             & 'TASKLIST /NH /FI \"' & $sImageName & ' EQ ' & $sAppID & '\" | FIND /I \"' & $sAppID & '\" >nul && GOTO START' & @CRLF _\r\n             & 'GOTO MOVE' & @CRLF _\r\n             & @CRLF _\r\n             & ':MOVE' & @CRLF _\r\n             & 'TASKKILL /F /FI \"' & $sImageName & ' EQ ' & $sAppID & '\"' & @CRLF _\r\n             & 'MOVE /Y ' & '\"' & $sScriptPath & '\"' & ' \"' & $sFileName & '\"' & @CRLF _\r\n             & 'IF EXIST \"' & $sScriptPath & '\" GOTO MOVE' & @CRLF _\r\n             & @CRLF _\r\n             & ':END' & @CRLF _\r\n             & $sRestart & @CRLF _\r\n             & 'DEL \"' & $sTempFileName & '\"'\r\n\r\n    Local Const $hFileOpen = FileOpen($sTempFileName, $FO_OVERWRITE)\r\n    If $hFileOpen = -1 Then\r\n        Return SetError(2, 0, 0)\r\n    EndIf\r\n    FileWrite($hFileOpen, $sData)\r\n    FileClose($hFileOpen)\r\n    Return Run($sTempFileName, @TempDir, @SW_HIDE)\r\nEndFunc   ;==>_SelfRename\r\n\r\n\r\n\r\n#pragma compile(Out, Example_1.exe)\r\n\r\n#include <MsgBoxConstants.au3>\r\n\r\n#include '_SelfRename.au3'\r\n\r\n; Start the SelfRename batch file if the executable doesn't match the same as Example.exe.\r\nIf _SelfRename('Example.exe', True) Then\r\n    MsgBox($MB_SYSTEMMODAL, '', 'The executable is currently called \"' & @ScriptName & '\", but it should be called \"Example.exe\"')\r\nElse\r\n    MsgBox($MB_SYSTEMMODAL, '', 'Successful, the application is called \"' & @ScriptName & '\"')\r\nEndIf"}}