Jump to content

Delete a directory from a batch file in that directory?


Recommended Posts

Is it possible to delete a folder using a batch file inside that folder? I tried:

echo >"%tmp%\uninstall-blahblah.cmd" rd /S /Q "C:\program Files\BlahBlah" 
"%tmp%\uninstall-blahblah.cmd"

This deletes the files in the folder but not the folder itself. Is what I am requesting impossible, or is there a workaround?

Link to comment
Share on other sites


Have it make a runonce entry to delete the folder and then make it self destruct.

ok her try this:

Place this at the very end of your bat file:

del %0

This will make it auto-destuct.

inside it place the runonce entry to delete the folder and the folder will be deleted on the next reboot.

Link to comment
Share on other sites

Thanks. I found a workaround to what I was trying to do.

For my application which I didn't fully describe, I found I can just use the registry instead of a batch file:


[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\BlahBlah]
"DisplayName"="BlahBlah"
"UninstallString"="cmd /c echo Windows Registry Editor Version 5.00>\"%TMP%\\BlahBlah.reg\"&echo.>>\"%TMP%\\BlahBlah.reg\"&echo [-HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\BlahBlah]>>\"%TMP%\\BlahBlah.reg\"&\"%systemroot%\\regedit\" /s \"%TMP%\\BlahBlah.reg\""

I will explain a little later where I am headed with this, probably in the Application Addons forum.

Edited by daddydave
Link to comment
Share on other sites

I have my doubts about your second post.

On an existing Windows installation, %TMP% refers to %USERPROFILE%\Local Settings\Temp.

During Windows setup, %TMP% refers to %SYSTEMROOT%\Temp as there are no user profiles yet (provided that Windows setup actually recognizes the %TMP% variable -- haven't tried it yet).

If your addon copies something into %TMP% during Windows setup, it will be located in %SYSTEMROOT%\Temp so there's no point in trying to delete it from %USERPROFILE%\Local Settings\Temp afterwards.

By the way... Windows setup purges the %SYSTEMROOT%\Temp folder right before the final reboot so you actually don't need to do anything. Just leave everything in %SYSTEMROOT%\Temp and Windows setup will do the rest, heheh :)

Edited by Tomcat76
Link to comment
Share on other sites

The reason I wanted to delete the folder that contained uninstall.cmd was that I wanted to refer to that batch file in the Uninstall registry key, so that the addon would appear in Add/Remove programs and could be uninstalled from there. This is for programs that don't have a setup program per se. After my first post, I have abruptly shifted strategy by choosing to put a string of commands in the UninstallString instead:

cmd /c command1 & command2 & command3

I also found to my surprise that command line redirection (the > and >> symbols) work on each command here as well (at least in Win2k), so I no longer have the original problem.

If your addon copies something into %TMP% during Windows setup

It won't, nor will it delete anything from the %TMP% folder. And those commands in the reg key will not get run during installation, they will get run when the user interactively clicks the Change/Remove button in the Add or Remove Programs applet.

I did not include rd /s /q %ProgramFiles%\BlahBlah in the chain of commands in the .reg file in Post #3, but should have that as well. It will also delete the shortcuts from All Users start menu. The only interaction with the %TMP% folder so far is that the code in UninstallString (which was going to be in a batch file) needs a place to create a .reg file that contains:

Windows Registry Editor Version 5.00

[-HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\BlahBlah]

in order to delete a registry key using REGEDIT /S.

That part works as far as being able to remove the entry from Add/Remove Programs interactively. I determined that running whatever is in the UninstallString doesn't remove the Uninstall registry entry automatically, it must be explicitly deleted by whatever is in UninstallString. For example if you put Notepad in the UninstallString, it runs Notepad but the program remains listed in Add or Remove Programs because its subkey under Uninstall is still there. Of course usually there is a fancy uninstaller listed there that is smart enough to remove the subkey, but not in my case

In my addon, install.cmd will have some code to create a reg file that creates the Uninstall value for the program. (The reg file in Post #3) That .reg file too will need to go somewhere, I suppose that will be %systemroot%\Temp, but again once REGEDIT /S is run, that file is not referred to again.

Edited by daddydave
Link to comment
Share on other sites

Here's a one liner:
START/B %COMSPEC% /C @RD/S/Q "%~DP0"

Make it the last line of your batch file!

Interesting..I'll take a look at that, even though it looks like I may not need what I originally requested.. Thanks

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...