Jump to content

Uninstall cmd template with self-delete current folder


Recommended Posts

I'm thinking of creating a cmd template script to self delete current folder after uninstalling application.

I would like to add some blocking code just in case it is launch from %programfiles%, %windir% or %windir% sub folder.

I figure out some basic code and thought if anyone here can help to polish the code a bit.

:: Uninstall cmd template with self-delete current folder
@echo off
Title Ensure current folder is not directly in %ProgramFiles%, %Windir% or %windir% sub directory

If "%~dp0"=="%programfiles%\" Echo You are directly in %~dp0 folder! Exit now!&&pause&&Exit
Goto Winfoldercheck
Goto Rootcheck

:Winfoldercheck
Set Cdir=%~dp0
Set cdir=%Cdir:~,10%
If %cdir%==%windir% Echo You are at %~dp0 folder! Exit now!&&pause&&exit

:Rootcheck
Set Rootdir=%~dp0
Set Rootdir1=%Rootdir:~3,1%
REM Echo 4th character is %Rootdir1%
If errorlevel 1 Echo You are at %~dp0 root folder! Exit now!&&pause&&exit

cls
Set Progname=AppnameXYZ
Title Uninstall %Progname%
set /P Reply=Do you wish to uninstall %Progname%? [y/n]: &&echo.
If %Reply%==n echo You have cancelled uninstall %Progname%, bye.&&Echo.&&pause&&Exit
If %Reply%==N echo You have cancelled uninstall %Progname%, bye.&&Echo.&&pause&&Exit
If %Reply%==y Goto selfremoveRD
If %Reply%==Y Goto selfremoveRD
If not %Reply%==Y echo You have entered an invalid key, bye.&&Echo.&&pause&&Exit

:selfremoveRD
cls
Echo You have selected ^"%Reply%^" to uninstall %Progname%.
Echo Removing application and this folder....(simulation only)&&Echo.
pause

:exit

Link to comment
Share on other sites


I'm not sure if I fully understand your intentions so I've quickly knocked up something which I hope is set out in a more systematic approach. (I've deliberately not used long lines or concatenation etc. in order to help readability)

@ECHO OFF
IF "%~dp0"=="%~d0\" (
ECHO=You are in the root directory.
GOTO ENDIT)
IF /I "%~dp0"=="%PROGRAMFILES%\" (
ECHO=You are in the Program Files directory.
GOTO ENDIT)
ECHO=%~dp0|FINDSTR/IB %SYSTEMROOT%\ >NUL &&(
ECHO=You are within the Windows directory tree.
GOTO ENDIT)
CLS
SET PN=AppnameXYZ
TITLE Uninstall %PN%
SET/P "R_=Do you wish to uninstall %PN%? [Y|N]: "
ECHO=
IF /I %R_:~,1%' NEQ Y' (
ECHO=You have cancelled uninstall %PN%
GOTO :ENDIT)
CLS
ECHO=You have selected ^"%R_%^" to uninstall %PN%.
PAUSE
ECHO=Removing this folder and it's sub-folders....
ECHO=
ECHO=RD/S/Q "%~dp0"
:ENDIT
ECHO=
ECHO=Closing!
PING -n 6 127.0.0.1 1>NUL

When you are satisfied that this will work, you can remove the first five characters from line number twenty four, (to test the actual directory removal).

NOTE: You do need to be careful that you do not confuse the Current Folder with that of the Running Script.

Edited by Yzöwl
simplified & updated code
Link to comment
Share on other sites

Thanks for looking into my script. I think I did not fully explain what is the purpose of the script.

I tried your code (in VM) in the following places (I called your code as WOWL.cmd)

(Attach is my code as Selfremovefolder.cmd)

C:\WOWL.cmd

or even D:\WOWL.cmd (as long as it is at root drive, not necessarily Systemrootdrive only )

C:\Program Files\WOWL.cmd

C:\WINDOWS\WOWL.cmd

C:\WINDOWS\Web\Wallpaper\WOWL.cmd

They are greeted with Do you wish to uninstall AppnameXYZ ? [Y|N]:

Which is what I do not wish it to show. They should be prompt with a cautious message and exit the script.

As my intended script is to remove directory which WOWL.cmd resides, plus all sub folder under it, running

under Windows folder can be dangerous and I wish to block it.

The only condition(s) it will run is when is runs from some non-critical windows locations such as

C:\Program Files\dummyfolder1\WOWL.cmd (which will remove dummyfolder1 folder after WOWL.cmd is run)

C:\Program Files\dummyfolder1\dummyfolder2\WOWL.cmd (which will remove dummyfolder2 folder after WOWL.cmd is run)

C:\dummytest5\WOWL.cmd (which will remove dummytest5 folder after WOWL.cmd is run)

I attached a working script (not polished code) Selfremovefolder.cmd containing the actual code to remove the current folder where Selfremovefolder.cmd resides.

Kinda dangerous code. Just have to be careful. That is why I need another pair or more eyes to help to check that there is no potential/unknown bugs which I may have overlook.

Selfremovefolder.cmd can be tested against C:\, C:\program files, C:\windows or sub folder of C:\windows like C:\WINDOWS\Web\Wallpaper

Can also create dummy folder in %programfiles% to see actual deletion

BTW, I tried googling for 'self remove folder cmd script' phase and doesn't seem to turn up any leads.

Selfremovefolder.cmd

Link to comment
Share on other sites

They are greeted with Do you wish to uninstall AppnameXYZ ? [Y|N]:

Which is what I do not wish it to show. They should be prompt with a cautious message and exit the script.

As my intended script is to remove directory which WOWL.cmd resides, plus all sub folder under it, running

under Windows folder can be dangerous and I wish to block it.

The only condition(s) it will run is when is runs from some non-critical windows locations such as

Sure, that is an EXAMPLE.

It has *some* PATHs checked.

You still need to add yourself the checks to avoid it running where you want it NOT to run, if for any reason:

  • "%PROGRAMFILES%" does not expand to "C:\Program Files"

(like a non-English version of the OS or different PATH settings)

or

  • "%SYSTEMROOT%" does not expand to "C:\Windows"

(like running the batch in a booted PE)

etc., etc.

BTW, I tried googling for 'self remove folder cmd script' phase and doesn't seem to turn up any leads.

What if you try googling for self-delete batch? ;)

http://www.catch22.net/tuts/selfdel

WARNING, these kind of things will SELF-DESTRUCT :ph34r: (and it won't even take 5 seconds to do so)

jaclaz

Link to comment
Share on other sites

Please see the updated code in my previous post

Side note:

In order to make this script more universal, I'd suggest you implement a method which prevents you from having to set %PN% manually inside each script!

Link to comment
Share on other sites

Please see the updated code in my previous post

Side note:

In order to make this script more universal, I'd suggest you implement a method which prevents you from having to set %PN% manually inside each script!

Thanks again. Tried your revised code in C:\Program Files\Vol Ctrl\New Folder\WOWL.cmd

I launch your script directly in windows explorer with Line 24 as RD/S/Q "%~dp0" : Can delete all files and sub folder except New Folder where WOWL.cmd resides. It is left with an empty folder. Any other way to remove this New Folder? (where the script resides)

The initial blocking/checking codes looks good, thanks. :yes:

(I run under XP, English)

Oh, yes. Something come to my mind. Possible to check whether script has admin right as well?

Regarding making script more universal, currently I have no idea how to do that. I will just set it manually if need to, lol

jaclaz, thanks for the link but I see most of the discussion are self-deleting the executing file, not current folder where the executing file resides.

Link to comment
Share on other sites

The parent/containing directory will not delete because you have it open in explorer. Try invoking it in a cmd console or from the run box and that directory will delete too!

As far as making it more universal goes, you could forget about using an app name and use the name of the directory instead.

This could be implemented easily:

@ECHO OFF
IF "%~dp0"=="%~d0\" (
ECHO=You are in the root directory.
GOTO ENDIT)
IF /I "%~dp0"=="%PROGRAMFILES%\" (
ECHO=You are in the Program Files directory.
GOTO ENDIT)
ECHO=%~dp0|FINDSTR/IB %SYSTEMROOT%\ >NUL &&(
ECHO=You are within the Windows directory tree.
GOTO ENDIT)
CLS
SETLOCAL
SET "PN=%~p0"
:LOOP
IF "%PN:*\=%" NEQ "" (SET "PN=%PN:*\=%"&&GOTO LOOP)
SET "PN=%PN:~,-1%"
TITLE Uninstall %PN%
SET/P "R_=Do you wish to uninstall %PN%? [Y|N]: "
ECHO=
IF /I %R_:~,1%' NEQ Y' (
ECHO=You have cancelled Uninstall %PN%
GOTO :ENDIT)
CLS
ECHO=You have selected ^"%R_%^" to uninstall %PN%.
PAUSE
ECHO=Removing this folder and it's sub-folders....
ECHO=
ECHO=RD/S/Q "%~dp0"
:ENDIT
ECHO=
ECHO=Closing!
PING -n 6 127.0.0.1 1>NUL

Link to comment
Share on other sites

As far as making it more universal goes, you could forget about using an app name and use the name of the directory instead.

Why I never think of that? :blushing: Wise thought :D

Link to comment
Share on other sites

Like alway good work Yzöwl :thumbup

I was wondering if you can pass arguments to cmd promt, like something like

this vbs script which with a little modification could do what you want.

Argumnent I used in the run box to test the script

C:\Users\Eddie\Desktop\Demo_DeleteFolder.vbs "Drive\Path_To_App\AppName.exe" ,"D:\Program Files (x86)"

C:\Users\Eddie\Desktop\Demo_DeleteFolder.vbs "Drive\Path_To_App\AppName.exe" ,"Drive\Path_To_App"

Script is coded to work only if 2 arguments are passed to it like the above, or 2 items are drag

and drop on to the script.

Save As Demo_DeleteFolder.vbs


Dim Arg
If Wscript.Arguments.Count = 2 Then
For Each Arg In WScript.Arguments
'-> App Path Name
If Right(InStr(Arg,"."),3) Or Right(InStr(Arg,"."),4) Or Right(InStr(Arg,"."),5) Then
WScript.Echo "File :" & Arg
Else
'-> Folder Path
If InStr(1,Arg,"WINDOWS",1) Or InStr(1,Arg,"Program Files",1) Then
WScript.Echo "Illegal Folder : " & Arg
Else
WScript.Echo "Folder :" & Arg
End If
End If
Next
'-> Various Error Messages
ElseIf Wscript.Arguments.Count = 1 Then
MsgBox "Not Enough Arguments Passed",4128,"One Argument"
WScript.Quit
ElseIf Wscript.Arguments.Count >= 3 Then
MsgBox "To Many Arguments For This Script",4128,"More Then 3 Argument"
WScript.Quit
Else
MsgBox "This Script Is Ment To Run With 2 Arguments",4128,"User Just Click Script"
End If

Link to comment
Share on other sites

I did something similar with AutoIT and I thought I found some secret trick! But basically, in order for my app to delete itself I have it write a batch file, save it to another folder, then the batch file kills the app and deletes it afterwards. Here is an excerpt:

_FileWriteLog("c:\temp\rem.bat","taskkill /f /im program.exe")
_FileWriteToLine("c:\temp\rem.bat", 1, "taskkill /f /im program.exe", 1)
_FileWriteToLine("c:\temp\rem.bat", 2, "del " & $StartupPath & "program.exe", 1)
_FileWriteToLine("c:\temp\rem.bat", 3, "c:\windows\system32\shutdown.exe -r -f -t 0", 1)
Run ( "c:\temp\rem.bat" )

Link to comment
Share on other sites

I did something similar with AutoIT

I have an autoit code for this too :yes:

Note: _SelfDelete func was not mine. It was shared in autoit forum.

;uninstall.au3
If ProcessExists ("ProgramName.exe") then ProcessClose ("ProgramName.exe")
FileDelete ("ProgramName.exe")

Call ("_SelfDelete")
Func _SelfDelete($iDelay = 0)
Local $sCmdFile

FileDelete(@TempDir & "\rem.bat")
$sCmdFile = 'ping -n ' & $iDelay & '127.0.0.1 > nul' & @CRLF _
& ':loop' & @CRLF _
& 'del "' & @ScriptFullPath & '"' & @CRLF _
& 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
& 'del ' & @TempDir & '\rem.bat'
FileWrite(@TempDir & "\rem.bat", $sCmdFile)
Run(@TempDir & "\rem.bat", @TempDir, @SW_HIDE)
EndFunc

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...