Jump to content

Exe compiled script self delete


Recommended Posts

I am using the below code:

Func _SelfDelete()
    Local $cmdfile
    FileDelete(@TempDir & "\dcp.cmd")
    $cmdfile = ':loop' & @CRLF _
    & 'del "' & @ScriptFullPath & '"' & @CRLF _
    & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
    & 'del ' & @TempDir & '\dcp.cmd'
    FileWrite(@TempDir & "\dcp.cmd", $cmdfile)
   ;Run(@TempDir & "\dcp.cmd", @TempDir, @SW_HIDE)
    Run(@TempDir & "\dcp.cmd", @TempDir)
EndFunc

It doesnt work on all computers.

I want to be able to do a check an if it is true to close the script and delete the exe file.

If VersionCheck() <> 1 Then 
    MsgBox(0,"System error","General version error.Please download latest version")
    _SelfDelete()
    Exit
EndIf

Please help.

Edited by Juvigy
Link to comment
Share on other sites

Looks like the file is in use for some reason and the cmc loop continues like for 5 min and then deletes the file. How can i ensure it is deleted right away?

For example how i can delete files in use ?

Link to comment
Share on other sites

  • 6 months later...

I have found this somewhere awhile ago. I DO NOT take credit for this but it works great at self destruction if compiled.

If @Compiled Then
        $sCMDLine = ':CheckFile' & @CRLF & _
                    'If exist "' & @ScriptFullPath & '" GOTO :DelFile' & @CRLF & _
                    'Goto :CheckFile' & @CRLF & _
                    ':DelFile' & @CRLF & _
                    'del /f /q "' & @ScriptFullPath & '"' & @CRLF & _
                    'del /f /q "' & @TempDir & '\~deltmp.bat"'
        $sCMDLine = _StringANSI2OEM($sCMDLine)
        $hFile = FileOpen(@TempDir & "\~deltmp.bat", 2)
        FileWrite($hFile, $sCMDLine)
        FileClose($hFile)
        Run(@TempDir & "\~deltmp.bat", @TempDir, @SW_HIDE)
    EndIf

Edit:

Forgot this function:

Func _StringANSI2OEM($strText)
    Local $buf = DllStructCreate("char["& StringLen($strText)+1 &"]")
    Local $ret = DllCall("User32.dll", "int", "CharToOem", "str", $strText, "ptr", DllStructGetPtr($buf))
    If Not(IsArray($ret)) Then Return SetError(1, 0, '')
    If $ret[0]=0 Then Return SetError(2, $ret[0], '')
    Return DllStructGetData($buf, 1)
EndFunc  ;==> _StringANSI2OEM
Edited by rogue5099
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...