views:

472

answers:

5

I have directory structure:

DIR
|-UNINSTALL.BAT
|-component_name
  |-source
  |-setup.exe
  |-uninst.bat
|-another_component_name
  |-source
  |-setup.exe
  |-uninst.bat
|-yet_another_component_name
  |-source
  |-setup.exe
  |-uninst.bat

and so on...

In every directory like "component_name" I have setup.exe file which installs current component to the palette component in Delphi. uninst.bat contains only this string:

"setup.exe" /uninstall

So I need to write UNINSTALL_ALL.bat in DIR that would run the uninst.bat in all component directories.

Thank you in advance.

A: 

That is kind of akward in a batch file. Though you could probably do it with the foreach statement. I would suggest though that you have a look at Powershell which will definitely give you the power to do this simply and a whole lot more if you want it.

David McEwing
One potential issue with Powershell is that it probably isn't installed on most boxes, whereas you can always run a batch file.OTOH, batch files suck horribly when trying to do complex things, so although this particular thing can be done without too much fuss, I can easily imaging running into a situation where you wish you'd used a real language.
Jason Creighton
True. But powershell is shipped as part of windows 7, windows 2008 server and is available via windows update for most other versions of windows, and is supported by Microsoft Product Support. So therefore in a controlled release environment should be possible to install. I think the benefits outweigh the hurdles of getting it installed.
David McEwing
+2  A: 

you could do it with this line:

 for /f %%a in ('dir /b /s uninst.bat') do call %%a

note that the '%%' is necessary for batch files. if you are testing this on the command line, only use one '%'

akf
That's don't work.One .bat runs another .bat, which runs .exe file with key...Maybe it can't be so at all?
phpcoder
that should work. what sort of errors are you seeing on the console? have you tested your uninst.bat standalone?
akf
A: 

You want to use the "for" construct. Something like this:

for %%i in (component_name another_component_name yet_another_component_name) do %%i\uninst.bat

The double-escaping (%%) is necessary if you put the "for" loop in a batch file. If you are just typing it out in a command prompt, only use 1 %.

Also, you may be able to use a wildcard to match against the directory names, if they follow some convention. Open up a command prompt and run "for /?" to see everything it can do...I believe there is a /d option to match against directories. That would look something like:

for /D %%d in (component_*) do %%d\uninst.bat

(obviously, adjust the wildcard to match your component directories.)

Jason Creighton
A: 

This should work:

FOR /F %%a IN ('dir /b /s uninst.bat') DO START /B %%a

if you want them to wait each other, use this:

FOR /F %%a IN ('dir /b /s uninst.bat') DO START /B /WAIT %%a
Camilo Martin
A: 

The way you describe your problem, you have only one level of sub-directories and you always call the same batch, from the root. Therefore:

Uninstall_all.cmd

@echo off
for /F "delims=" %%d in ('dir /b /ad') do cd "%%d"& start /b /w ..\uninstall.bat& cd ..

Should do the trick.

Jay