Jump to content

Search the Community

Showing results for tags 'rename running exe'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • General
    • Announcements and Site News
    • Administration
  • AutoIt v3
    • AutoIt Help and Support
    • AutoIt Technical Discussion
    • AutoIt Example Scripts
  • Scripting and Development
    • Developer General Discussion
    • Language Specific Discussion
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Categories

  • AutoIt Team
    • Beta
    • MVP
  • AutoIt
    • Automation
    • Databases and web connections
    • Data compression
    • Encryption and hash
    • Games
    • GUI Additions
    • Hardware
    • Information gathering
    • Internet protocol suite
    • Maths
    • Media
    • PDF
    • Security
    • Social Media and other Website API
    • Windows
  • Scripting and Development
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Member Title


Location


WWW


Interests

Found 1 result

  1. Sometimes you don't want the user to rename the executable to their choice. This is why I created the function _SelfRename, to ensure the executable stays exactly as you want it. Function: Using a Batch script. Save as _SelfRename.au3 #include-once #include <FileConstants.au3> #include <StringConstants.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: __SelfRename ; Description ...: Rename the running executable to a different filename. ; Syntax ........: _SelfRename($sFileName[, $fRestart = Default[, $iDelay = 5[, $fUsePID = Default]]]) ; Parameters ....: $sFileName - Filename the executable should be called e.g. Example.exe or Example ; $fRestart - [optional] Restart the application (True) or to not restart (False) after overwriting. Default is False. ; $iDelay - [optional] An integer value for the delay to wait (in seconds) before stopping the process and deleting the executable. ; If 0 is specified then the batch will wait indefinitely until the process no longer exits. Default is 5 (seconds). ; $fUsePID - [optional] Use the process name (False) or PID (True). Default is False. ; Return values .: Success - Returns the PID of the batch file. ; Failure - Returns 0 & sets @error to non-zero ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _SelfRename($sFileName, $fRestart = Default, $iDelay = 5, $fUsePID = Default) If @Compiled = 0 Then Return SetError(1, 0, 0) EndIf Local Const $iPosition = StringInStr($sFileName, '.', $STR_NOCASESENSEBASIC, -1) - 1 If $iPosition Then $sFileName = StringLeft($sFileName, $iPosition) EndIf Local $sAppID = @ScriptName If $sFileName & '.exe' = $sAppID Then Return SetError(3, 0, 0) EndIf $sFileName = @ScriptDir & '\' & $sFileName & '.exe' Local $sTempFileName = @ScriptName $sTempFileName = StringLeft($sTempFileName, StringInStr($sTempFileName, '.', $STR_NOCASESENSEBASIC, -1) - 1) While FileExists(@TempDir & '\' & $sTempFileName & '.bat') $sTempFileName &= Chr(Random(65, 122, 1)) WEnd $sTempFileName = @TempDir & '\' & $sTempFileName & '.bat' If $iDelay = Default Then $iDelay = 5 EndIf Local $sDelay = '' $iDelay = Int($iDelay) If $iDelay > 0 Then $sDelay = 'IF %TIMER% GTR ' & $iDelay & ' GOTO DELETE' EndIf Local $sImageName = 'IMAGENAME' If $fUsePID Then $sAppID = @AutoItPID $sImageName = 'PID' EndIf Local $sCmdLineRaw = '' If $CmdLineRaw <> '' Then $sCmdLineRaw = ' ' & $CmdLineRaw EndIf Local $sRestart = '' If $fRestart Or $fRestart = Default Then $sRestart = 'START "" "' & $sFileName & '"' & $sCmdLineRaw EndIf $sCmdLineRaw = '' Local Const $iInternalDelay = 2, _ $sScriptPath = @ScriptFullPath Local Const $sData = 'SET TIMER=0' & @CRLF _ & ':START' & @CRLF _ & 'PING -n ' & $iInternalDelay & ' 127.0.0.1 > nul' & @CRLF _ & $sDelay & @CRLF _ & 'SET /A TIMER+=1' & @CRLF _ & @CRLF _ & 'TASKLIST /NH /FI "' & $sImageName & ' EQ ' & $sAppID & '" | FIND /I "' & $sAppID & '" >nul && GOTO START' & @CRLF _ & 'GOTO MOVE' & @CRLF _ & @CRLF _ & ':MOVE' & @CRLF _ & 'TASKKILL /F /FI "' & $sImageName & ' EQ ' & $sAppID & '"' & @CRLF _ & 'MOVE /Y ' & '"' & $sScriptPath & '"' & ' "' & $sFileName & '"' & @CRLF _ & 'IF EXIST "' & $sScriptPath & '" GOTO MOVE' & @CRLF _ & @CRLF _ & ':END' & @CRLF _ & $sRestart & @CRLF _ & 'DEL "' & $sTempFileName & '"' Local Const $hFileOpen = FileOpen($sTempFileName, $FO_OVERWRITE) If $hFileOpen = -1 Then Return SetError(2, 0, 0) EndIf FileWrite($hFileOpen, $sData) FileClose($hFileOpen) Return Run($sTempFileName, @TempDir, @SW_HIDE) EndFunc ;==>_SelfRenameExample use of Function: #pragma compile(Out, Example_1.exe) #include <MsgBoxConstants.au3> #include '_SelfRename.au3' ; Start the SelfRename batch file if the executable doesn't match the same as Example.exe. If _SelfRename('Example.exe', True) Then MsgBox($MB_SYSTEMMODAL, '', 'The executable is currently called "' & @ScriptName & '", but it should be called "Example.exe"') Else MsgBox($MB_SYSTEMMODAL, '', 'Successful, the application is called "' & @ScriptName & '"') EndIf
×
×
  • Create New...