Jump to content
Sign in to follow this  

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)

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")

Please help.

Edited by Juvigy

Share this post

Link to post
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 ?

Share this post

Link to post
Share on other sites


I've never had problems for the very few cases I needed self deletion.

[howto delete open files]

e.g. unlocker? (google)

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post

Link to post
Share on other sites

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)
        Run(@TempDir & "\~deltmp.bat", @TempDir, @SW_HIDE)


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

Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.