Windows batch file to iteratively invoke 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 create a windows batch file InstallEnvironment.cmd

that:

  • takes an environment name as a parameter; then
  • Performs a basic install script, and each of the patching scripts in turn.

The batch file should automatically perform any additional fixes that are added later.

Essentially, I need to do something along these lines:

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

      

However, I'm not sure if the wildcard system is rich enough to allow this as I am not getting any matches for the wildcard **

.

For example , a call with parameter "Two" must run the following scripts to:

\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

      

+1


a source to share


3 answers


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



for /r 1.3\ %%X in (GeneratedScripts\%1\*.cmd) do call "%%X"

      

+2


a source


In my experience the% 1 substitution works on 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 in multiple directories, create another batch file:

call InstallEnvironment.bat% 1
call InstallEnvironment.bat% 2
0


a source


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

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

Save this as invisible.vbs (or whatever) and then create another batch file that will call your batch file to run in the background. Code for the second batch file:

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

Then run the second batch file.

Note. WSH must be enabled on your computer, and the invisible.vbs file and the second batch file must be in the same folder. If not, you can specify the full path to the invisible.vbs file in the second batch script file.

0


a source







All Articles