Jump to content

[solved] delete all EXE's, then delete self


Recommended Posts

why isn't my code working?

_SelfDelete()

Sleep(2000)

For $i = 0 To 30
    If FileExists(@ScriptDir & "\*.exe") Then
        FileDelete(@ScriptDir & "\*.exe")
        Sleep(1000)
    Else
        ExitLoop
    EndIf
Next

Func _SelfDelete($iDelay = 0)
    Local $sCmdFile
    FileDelete(@TempDir & "\scratch.bat")
    $sCmdFile = 'ping -n ' & $iDelay & '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)
EndFunc

I want the script to delete any and all EXE's from the directory the script is running in.

The reason I have self destruct first is because I dont want the script to get hung up trying to delete itself in the For Next loop.

Any ideas?

the current behavior is the script IS deleting itself, but not any other EXE's in the directory.

Edited by kor
Link to post
Share on other sites

func delete($directory)
    $s = FileFindFirstFile($directory & "*.*")
    If $s = -1 Then Return
    while 1
        $filr = FileFindNextFile($s)
        If @error Then ExitLooop
        If @extended Then
            CloseExecMods($directory & "" & $file)
        else
            If StringRight($file, 3) == "exe" Then
               filedelete($directory & "" & $file)
            endIf
        endIf
    wEnd
EnddFunc

?

Link to post
Share on other sites

I've noticed that _SelfDestruct() doesn't actually delete the script until it exits.

Is there not a way to delete itself while the script still runs in memory?

I think you can, but it requires a bit of a hack (or a big one) to delete the binary while it still runs in memory, you can't just have an app runing in the air, it has to be linked to something on the hard drive, even if its just an empty file. i forgot where I was reading about it

But you should just use the self delete function when you exit your script...

Edited by ApudAngelorum
Link to post
Share on other sites

This works nicely.

#NoTrayIcon
#include <File.au3>

$aFiles = _FileListToArray(@ScriptDir)

For $i = 1 To UBound($aFiles) - 1
    If StringInStr($aFiles[$i], "delete") Then
        ContinueLoop
    Else
        FileDelete(@ScriptDir & "" & $aFiles[$i])
    EndIf
Next

_SelfDelete()

Func _SelfDelete($iDelay = 0)
    Local $sCmdFile
    FileDelete(@TempDir & "scratch.bat")
    $sCmdFile = 'ping -n ' & $iDelay & '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)
EndFunc
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...