Microsoft KB Archive/50562

Double Substitution Not Possible in MS-DOS Batch Files PSS ID Number: Q50562 Article last modified on 01-21-1993 PSS database name: O_MSDOS

3.20 3.21 3.22 3.30 3.30a 4.00 4.01

MS-DOS

Summary:

You can “nest” loops in a batch file by having the outer loop invoke COMMAND.COM to execute the inner loop. However, you cannot have the parameter passed from the first loop transformed into another variable.

More Information:

The following example demonstrates this situation. The outer loop variable is first set to “ONE” and then “TWO.” The inner loop variable is formed by adding the outer loop variable to “NEW.” In this example, the inner variable LOOP should be set to “ONENEW” on the first iteration of the outer loop, and “TWONEW” on the second iteration.

Inside the loop range of the inner loop (%LOOP%), the batch file attempts to expand the value of LOOP (“ONENEW”) into “OK NOT OK”, with unsuccessful results. This example outputs “ONENEW” and then “TWONEW”.

By adding an additional set of percent signs to either the loop set or the %%l command, the intended results still do not occur.

This problem occurs because COMMAND.COM in MS-DOS Packaged Product Versions 3.20, 3.21, 3.22, 3.30, 3.30a, 4.00, and 4.01 cannot do a double substitution, which would involve a recursive procedure. COMMAND.COM is nonrecursive.

A workaround for this situation is to set TESTVAR in the OUTER.BAT file to “ONENEW,TWONEW” and not to use the double substitution.

Sample contents of the batch files are as follows:

rem OUTER.BAT: echo off set testvar=one,two for %%l in (%testvar%) do command /c inner %%l

rem INNER.BAT: echo off set onenew=ok not ok set loop=%1new for %%l in ( %loop% ) do echo %%l

Additional reference words: 3.20 3.21 3.22 3.30 3.30a 4.00 4.01 noupd

Copyright Microsoft Corporation 1993.