Geej Posted October 4, 2010 Share Posted October 4, 2010 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 offTitle Ensure current folder is not directly in %ProgramFiles%, %Windir% or %windir% sub directoryIf "%~dp0"=="%programfiles%\" Echo You are directly in %~dp0 folder! Exit now!&&pause&&ExitGoto WinfoldercheckGoto Rootcheck:WinfoldercheckSet Cdir=%~dp0Set cdir=%Cdir:~,10%If %cdir%==%windir% Echo You are at %~dp0 folder! Exit now!&&pause&&exit:RootcheckSet Rootdir=%~dp0Set Rootdir1=%Rootdir:~3,1%REM Echo 4th character is %Rootdir1%If errorlevel 1 Echo You are at %~dp0 root folder! Exit now!&&pause&&exitclsSet Progname=AppnameXYZTitle 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&&ExitIf %Reply%==N echo You have cancelled uninstall %Progname%, bye.&&Echo.&&pause&&ExitIf %Reply%==y Goto selfremoveRDIf %Reply%==Y Goto selfremoveRDIf not %Reply%==Y echo You have entered an invalid key, bye.&&Echo.&&pause&&Exit:selfremoveRDclsEcho 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 More sharing options...
Yzöwl Posted October 4, 2010 Share Posted October 4, 2010 (edited) 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 OFFIF "%~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)CLSSET PN=AppnameXYZTITLE 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)CLSECHO=You have selected ^"%R_%^" to uninstall %PN%.PAUSEECHO=Removing this folder and it's sub-folders....ECHO=ECHO=RD/S/Q "%~dp0":ENDITECHO=ECHO=Closing!PING -n 6 127.0.0.1 1>NULWhen 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 October 5, 2010 by Yzöwl simplified & updated code Link to comment Share on other sites More sharing options...
Geej Posted October 5, 2010 Author Share Posted October 5, 2010 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.cmdor even D:\WOWL.cmd (as long as it is at root drive, not necessarily Systemrootdrive only )C:\Program Files\WOWL.cmdC:\WINDOWS\WOWL.cmdC:\WINDOWS\Web\Wallpaper\WOWL.cmdThey 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, runningunder 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\WallpaperCan also create dummy folder in %programfiles% to see actual deletionBTW, 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 More sharing options...
jaclaz Posted October 5, 2010 Share Posted October 5, 2010 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, runningunder 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 asSure, 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/selfdelWARNING, these kind of things will SELF-DESTRUCT (and it won't even take 5 seconds to do so)jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted October 5, 2010 Share Posted October 5, 2010 Please see the updated code in my previous postSide 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 More sharing options...
Geej Posted October 6, 2010 Author Share Posted October 6, 2010 Please see the updated code in my previous postSide 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.cmdI 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. (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, loljaclaz, 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 More sharing options...
Yzöwl Posted October 6, 2010 Share Posted October 6, 2010 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 OFFIF "%~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)CLSSETLOCALSET "PN=%~p0":LOOPIF "%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)CLSECHO=You have selected ^"%R_%^" to uninstall %PN%.PAUSEECHO=Removing this folder and it's sub-folders....ECHO=ECHO=RD/S/Q "%~dp0":ENDITECHO=ECHO=Closing!PING -n 6 127.0.0.1 1>NUL Link to comment Share on other sites More sharing options...
Geej Posted October 7, 2010 Author Share Posted October 7, 2010 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? Wise thought Link to comment Share on other sites More sharing options...
gunsmokingman Posted October 8, 2010 Share Posted October 8, 2010 Like alway good work Yzöwl I was wondering if you can pass arguments to cmd promt, like something likethis vbs script which with a little modification could do what you want.Argumnent I used in the run box to test the scriptC:\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 dragand drop on to the script.Save As Demo_DeleteFolder.vbsDim 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 More sharing options...
Tripredacus Posted October 11, 2010 Share Posted October 11, 2010 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 More sharing options...
Geej Posted October 12, 2010 Author Share Posted October 12, 2010 I did something similar with AutoITI have an autoit code for this too Note: _SelfDelete func was not mine. It was shared in autoit forum. ;uninstall.au3If ProcessExists ("ProgramName.exe") then ProcessClose ("ProgramName.exe")FileDelete ("ProgramName.exe")Call ("_SelfDelete")Func _SelfDelete($iDelay = 0)Local $sCmdFileFileDelete(@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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now