如何从循环中的.bat运行* .exe / key

时间:2009-06-21 21:21:47

标签: windows batch-file

我有目录结构:

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

依旧......

在像“component_name”这样的每个目录中,我都有setup.exe文件,它将当前组件安装到Delphi中的调色板组件中。 uninst.bat仅包含此字符串:

"setup.exe" /uninstall

所以我需要在UNINSTALL_ALL.bat中编写DIR来运行所有组件目录中的uninst.bat。

提前谢谢。

5 个答案:

答案 0 :(得分:2)

你可以用这一行来做:

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

请注意,批处理文件需要'%%'。如果您在命令行上测试它,只使用一个'%'

答案 1 :(得分:0)

这在批处理文件中是一种尴尬。虽然您可以使用foreach语句来完成它。我建议你看一下 Powershell ,如果你需要的话,它肯定会让你有能力做到这一点,还有更多。

答案 2 :(得分:0)

您想使用“for”构造。像这样:

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

如果将“for”循环放在批处理文件中,则必须进行双重转义(%%)。如果您只是在命令提示符下键入它,则只使用1%。

此外,如果符合某些约定,您可以使用通配符来匹配目录名称。打开命令提示符并运行“for /?”看到它可以做的一切...我相信有一个/ d选项来匹配目录。这看起来像是:

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

(显然,调整通配符以匹配您的组件目录。)

答案 3 :(得分:0)

这应该有效:

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

如果你想让他们互相等待,请使用:

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

答案 4 :(得分:0)

您描述问题的方式,您只有一个级别的子目录,并且您始终从根调用同一批次。因此:

Uninstall_all.cmd

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

应该做的伎俩。