views:

810

answers:

3

Consider a directory structure containing the following files:

\1.3\Baseline\GeneratedScripts\One\FullInstall.cmd
\1.3\Baseline\GeneratedScripts\Two\FullInstall.cmd
\1.3\Baseline\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Three\FullInstall.cmd

I would like to construct a Windows batch file InstallEnvironment.cmd which:

  1. Takes an environment name as a parameter; then
  2. Executes the baseline install script, and each of the patch scripts in turn.

The batch file should automatically execute any additional patches that are added later.

Essentially I need to do something along the lines of this:

for %%_ in (1.3\**\GeneratedScripts\%%1\FullInstall.cmd) do cal %%_

However I'm not sure the wildcard system is rich enough to allow this as I don't get any matches for the ** directory wildcard.

For example, calling with the parameter "Two" should execute the following scripts, in order:

\1.3\Baseline\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Two\FullInstall.cmd
+2  A: 

This will execute all the *.cmd files in the sub folders based on the argument:

for /r 1.3\ %%X in (GeneratedScripts\%1\*.cmd) do call "%%X"
Iain
Ah fantastic. I missed the "/r" switch when reading the "for" documentation.
Daniel Fortunov
Baseline should come before the patches naturally, since (I assume) the file listing comes out in alphabetical order.
Daniel Fortunov
A: 

In my experience, the %1 substitution works within directory names.

This should work:

InstallEnvironment.bat:

\1.3\Baseline\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\%1\FullInstall.cmd

Edit this batch file to add additional patches in order, and it works. If you need to run the same batch file on multiple directories, create another batch file:

call InstallEnvironment.bat %1
call InstallEnvironment.bat %2
hapes
A: 

If you want to run a batch file in the background, use a vbs file to run that bat file in background instead. Here is the code:

CreateObject("Wscript.Shell").Run"""" & Wscript.Arguments(0)& """",0,False

Save this exactly as invisible.vbs (or anything) and then make another batch file which will call your batch file to run it in background. The code for the second batch file is:

wscript.exe "invisible.vbs" "Your_Batch_File.bat"

Then run the second batch file.

Note: WSH should be enabled on your computer, and the invisible.vbs file and the second batch file should be in the same folder. If not then you can give the full path to the invisible.vbs file in the 2nd batch file's script.