Daniel Fortunov
Daniel Fortunov

Reputation: 44333

Windows batch file for iterative invocation of other batch files

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

Upvotes: 1

Views: 1323

Answers (3)

Rushil
Rushil

Reputation:

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.

Upvotes: 0

Iain
Iain

Reputation: 10749

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"

Upvotes: 2

hapes
hapes

Reputation: 105

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

Upvotes: 0

Related Questions