Zediiiii
Zediiiii

Reputation: 780

Passing a Batch Variable Into a Nested Loop Correctly - Delayed Expansion of Variables

I'm attempting to make a script that creates a set of folders from list.txt (a list of file directory names separated by carriage returns), with each folder containing a particular number of subdirectories named by the user.

This script does exactly that and works correctly:

@echo off
setlocal enableextensions

set /P q="How many subdirectories would you like to add in each folder?[0/1/2/3]"
if /I "%q%" EQU "0" goto :somewhere0
if /I "%q%" EQU "1" goto :somewhere1
if /I "%q%" EQU "2" goto :somewhere2
if /I "%q%" EQU "3" goto :somewhere3
if errorlevel 1 goto :problem

:somewhere0
echo All done! Press any key to exit.
pause
exit

...(code cut for brevity)

:somewhere3
set /P c="What is the name of the first subdirectory?"
for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%c"\"%%d"
set /P c="What is the name of the second subdirectory?"
for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%c"\"%%d"
set /P c="What is the name of the third subdirectory?"
for /F "tokens=1 delims=," %%d IN (list.txt) DO if not exist %%d md "%%d" cd "%%d" md "%%c" cd ..
if errorlevel 1 goto :problem
goto :somewhere0
...

You get the picture. However, I'm attempting to do this in a loop instead of hard-coding each instance. The code I've developed looks like this:

@echo off
setlocal enableextensions
set "var=string"

set /P q="How many subdirectories would you like to add in each folder?"

for /l %%x in (1, 1, %q%) do (
   set /P c="What is the name of subdirectory %%x?"
    for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%d\%c%"
)

Trying to echo the variable %%d or %c% in the middle of the loop doesn't yield anything. Stranger, the script actually creates the top level directories - ie, this code runs

for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%d

but the subdirectories do not show up. Suggestions?

Note that I don't honestly know why the first working code demands this particular order of % signs, but that is the only way I could get it to work.

Upvotes: 0

Views: 291

Answers (1)

user7818749
user7818749

Reputation:

enabledelayedexpansion is what you want here.

@echo off
setlocal enableextensions enabledelayedexpansion
set "var=string"

set /P q="How many subdirectories would you like to add in each folder? "

for /l %%x in (1, 1, %q%) do (
    set /P c="What is the name of subdirectory %%x? "
    for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%d\!c!"
 )

from cmd.exe if you run setlocal /? you will get some more help, but in short, enabledelayedexpansion enables delayed environment variable expansion. It will cause variables within a batch file to be expanded at execution time rather than at parse time. Can also be enabled as CMD /V:ON on commandline, and not in Batch.

You basically tell the command processor to delay the resolution of variables until it executes them. % is substituded with ! to show which variables are to be used in the expansion.

Upvotes: 1

Related Questions