How to Uninstall from a temp directory?

I've seen vendors place an Uninstall link in their Start\Programs folder.  The obvious question is, how does this program delete itself?  I assume it copies itself to a temp directory and runs from there.  But that opens two more questions. 1) What temp directory can be used that will automatically clear itself on the next boot? 2) How does the program transfter control to the temp version after copying itself?

Thanks for any ideas?
Who is Participating?
kapesConnect With a Mentor Commented:
in your example only if you had kept...

rmdir /s /q c:\"program files"\myfolder

it would have worked...

%0 gives the actual batch file name...

%~d0 gives its drive letter  =    c:
%~p0 gives its path except drive letter  = \program files\myfolder  (which has a SPACE, thats the problem)

so %~dp0 would give c:\program files\myfolder
it didn't work because path has SPACE in between when combined...

So put "%~dp0"   in quotes...

The uninstall function used by many programs is standard utility provided by the packaging softwate used to package the software in the first place, so that it can be installs in automated way...

So... the program which is actually uninstalling the software is not the part of any functionality provided by software itself...

Commonly used .... is installshield... which creates a logfile while installing a particular software, specifying the instructions on how to "uninstall" a particular software..

Temp directory is populated... while installing the software, and not while uninstalling... :-)
U have to keep a batch file running at logon or something to keep the temp directory empy.

generally putting ... following commands in the batch file will clear the temp directory

rmdir /s /q %temp%
rmdir /s /q %tmp%

stevaAuthor Commented:
In my case I wrote the code to do the install (copy a file into the drivers directory and create some registry entries) and now I just want to provide an easy way for the user undo that. InstallShield is like the Queen Mary when all I want is a row boat.  I guess I could have the Uninstall link display a message  to insert the installation CD and then run the Uninstall program from there. I was hoping, though, that I could actually do the uninstall without putting the CD back in. Any thoughts on how I might copy the uninstall program to %temp% and transfer control to it -  in a way that lets me then delete the code that did the copy and transferred control?

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

your question, doesn't say anything about your requirement !!!


Now if your application can be removed by just deleting the folder in question and removing registry entries..
you can write a batch file and put that batch file in same directory as the program...
so.. when you are running the uninstall batch file... the batch file is in Memory so its actual physical file can be deleted..

To prove it..
Create a blank folder anywhere.. and in it..
create batch file which contains.. next two lines... it will delete the folder in which it placed (i.e. \t1\) alongwith itself...

@echo off
rmdir /s /q %~d0%~p0 1>nul 2>nul

stevaAuthor Commented:
Ok, this sounds intersting.  Is there a fundamental difference here between a batch file and an exe file?

stevaAuthor Commented:
By that  I mean, could an exe also delete its disk copy?

Could you explain the syntax of your rmdir line and/or point to some documentation?  What are the %~d0% and %~p0% variables?

You asked about my "requirements."  If I delete the registry entries I made, the driver I loaded, and the files in ...Program Files\MyProduct there'll be no trace left of me on the machine.  The only part I was questioning was removing the uninstall.exe from the ...Program Files\MyProduct folder.

no it won't allow self-deletion by EXE..

OS treats them differently,
 For EXE, it will load it into memory and lock it on DISK.. so can't delete itself.. leaving the folder in which it is stored intact with it...

ON the other end, batch file is... sequence of command to be executed by "Command Processor" on the OS, generally cmd.exe on NT and above....

SO nothing is locked on DISK... so it can delete it.. the BATCH files... essentially CMD.EXE is deleting the batch file... :-)

you will find the details of variable here... Look for "FOR" command....

I would suggest... in your uninstall.exe you delete the registry and other things as usual.. and at the end
Create a batch file like what i have shown and run.. it... or...schedule it to run on next reboot in "RUNONCE" key in registry...
so it will run only once on next reboot and it will delete your directory with uninstall.exe and itself...
stevaAuthor Commented:
First, thanks for the link to the great Batch tutorial.  I've been working from a few pages out of a DOS 6.2 book.

Following your rmdir example above, I've created a batch file called cleanup.bat with the two lines:

del C:\"Program Files"\MyFolder\cleanup.bat
rmdir C:\"Program Files"\MyFolder

and  put it into the C:\Program Files\MyFolder. I then call it from the RunOnce key(actually "Run" so I don't have to keep putting the entry back in each try).  It deletes the cleanup.bat file ok, but it won't delete the MyFolder directory.

I tried then using exactly what you coded above:
rmdir /s /q %~d0%p0 1>nul 2>nul

But this doesn't run as is.  It seems that it's expecting an input parameter, which you're then expanding to a drive letter and a path. But what input does it want?  And how does the mandatory ":\" get inserted between the driver letter and the rest of the path?


stevaAuthor Commented:
You're right.  My example does work if I just use

rmdir /s /q c:\"program files"\myfolder

without preceding it with a line to delete the file in myfolder. I realize I don't need the del line since the rmdir removes all files as well as the directory, but why did the del line stop the rmdir from working?

In any case, you've solved the problem so I released the points to you.


the delete command needs to be last one in the batch file... ;-)

I said.. it doesn't lock the file...

it doesn't  PREFETCH the file in the memory....

I am glad, I was able to help you....
All Courses

From novice to tech pro — start learning today.