Understanding batch for loops with in (%variable1%, %variable2%…)












0















I have some tools that I'm trying to batch script on the back end of an MDT server. One of those tools is DISM/ImageX. What I'm trying to automate is a script that checks for the folders so that DISM can extract the individual WIM files and then I can use another utility to slipstream updates.



Not into WSUS. Nor SCCM. I'm aware that it can be done with them, but I'm looking to keep my deployment times as close to 15-20 min each for the most up to date version of a given OS.



I have a lot of my back end tools working correctly, but I'm having a problem condensing this part of my batch file down so that it uses a for loop to check the directories instead of having the same code repeated over and over.



I never really got into batch scripting, but hey, it seems to do almost every trick I can think of and i'm always looking for ways to optimize my code.



Perhaps somebody might be able to see what I'm trying to do here with my batch file, tell me what I'm getting wrong?



I've written it out the long way but I want to condense this code down with =< 1 for loop.



setlocal
set EnabledDelayedExpansion
set _d=\cp
set 3d_=G:DISM
set 3d7a=%3d_%7
set 3d7b=%3d_%77.1
set 3d7c=%3d_%77.2
set 3d7d=%3d_%77.3
set 3d7e=%3d_%77.4
set 3d8a=%3d_%8
set 3d8b=%3d_%88.1
set 3d8c=%3d_%88.2
set 3d8d=%3d_%88.3
set 3d8e=%3d_%88.4
set 3dxa=%3d_%10
set 3dxb=%3d_%1010.1
set 3dxc=%3d_%1010.2
set 3dxd=%3d_%1010.3
set 3dxe=%3d_%1010.4
goto :3d7f

:3d7f
for /f "tokens=5 delims= " %%a in ('%3d7a% %3d7b% %3d7c% %3d7d% %3d7e%') do
(
if not exist %%a (
mkdir %%a)
goto :3d8f

:3d8f
for /f "tokens=5 delims= " %%a in ('%3d8a% %3d8b% %3d8c% %3d8d% %3d8e%') do
(
if not exist %%a (
mkdir %%a)
goto :3dxf

:3dxf
for /f "tokens=5 delims= " %%a in ('%3dxa% %3dxb% %3dxc% %3dxd% %3dxe%') do
(
if not exist %%a (
mkdir %%a)
goto :exit
)


Looking to create the directories listed in the set variables up top, using an iterative for loop that checks if the directories are there, and if not then it creates them.



Each variable step up localizes the proper target directory in another global batch file I am using to eventually distribute to other MDT servers, this is the section I'm working on now.




  • Edited the mistakes noted by Squashman @ 5:34EST 1/1/19 (Thanks dude)










share|improve this question




















  • 2





    What is this: &&a? Did you mean to do this: %%a? Regardless of that, you do not need to check for the existence of a directory before you try to create it. You can just create the directory and redirect the error to the nul device. You are also missing a closing parentheses for each of the FOR commands.

    – Squashman
    Jan 1 at 18:32













  • Yes. And yes, thank you. Create the directory and redirect the error to the nul device? Didn't think of that... I can read a lot of programming but when it comes to writing it, my scope is rather limited. I'm working on that though, thanks for the correction and tip!

    – Michael Cook
    Jan 1 at 22:17











  • basically... what is not listed in the script is a "what workstation do you want to use for DISM?" since the tool I use takes a bit of hardware/time to process and I don't want to use the server since it's a bit older. trying to use some pushd and popd but that didn't work. but your explanation might help. basically, the batch script has a lot of other sections that are all allowed through an elevation script. wish I was using a domain but it's all workgroup based at this time. Besides that though, because some of the commands get really long, the variables shorten a lot of the code.

    – Michael Cook
    Jan 1 at 22:46











  • be aware that pushd just sets the working folder, it does not push the workload (it's still the server that runs the script). You'd need something like PSEXEC to actually transfer the workload.

    – Stephan
    Jan 2 at 8:43











  • instead of psexec, i was thinking of using some powershell commands, or just xcopying the files and workload, still learning a bit of how to get things transferred back and forth from the script, although MDT is doing most of this work, i'm just writing this as a fallback script just in case. like LGPOs and such

    – Michael Cook
    Jan 3 at 18:01
















0















I have some tools that I'm trying to batch script on the back end of an MDT server. One of those tools is DISM/ImageX. What I'm trying to automate is a script that checks for the folders so that DISM can extract the individual WIM files and then I can use another utility to slipstream updates.



Not into WSUS. Nor SCCM. I'm aware that it can be done with them, but I'm looking to keep my deployment times as close to 15-20 min each for the most up to date version of a given OS.



I have a lot of my back end tools working correctly, but I'm having a problem condensing this part of my batch file down so that it uses a for loop to check the directories instead of having the same code repeated over and over.



I never really got into batch scripting, but hey, it seems to do almost every trick I can think of and i'm always looking for ways to optimize my code.



Perhaps somebody might be able to see what I'm trying to do here with my batch file, tell me what I'm getting wrong?



I've written it out the long way but I want to condense this code down with =< 1 for loop.



setlocal
set EnabledDelayedExpansion
set _d=\cp
set 3d_=G:DISM
set 3d7a=%3d_%7
set 3d7b=%3d_%77.1
set 3d7c=%3d_%77.2
set 3d7d=%3d_%77.3
set 3d7e=%3d_%77.4
set 3d8a=%3d_%8
set 3d8b=%3d_%88.1
set 3d8c=%3d_%88.2
set 3d8d=%3d_%88.3
set 3d8e=%3d_%88.4
set 3dxa=%3d_%10
set 3dxb=%3d_%1010.1
set 3dxc=%3d_%1010.2
set 3dxd=%3d_%1010.3
set 3dxe=%3d_%1010.4
goto :3d7f

:3d7f
for /f "tokens=5 delims= " %%a in ('%3d7a% %3d7b% %3d7c% %3d7d% %3d7e%') do
(
if not exist %%a (
mkdir %%a)
goto :3d8f

:3d8f
for /f "tokens=5 delims= " %%a in ('%3d8a% %3d8b% %3d8c% %3d8d% %3d8e%') do
(
if not exist %%a (
mkdir %%a)
goto :3dxf

:3dxf
for /f "tokens=5 delims= " %%a in ('%3dxa% %3dxb% %3dxc% %3dxd% %3dxe%') do
(
if not exist %%a (
mkdir %%a)
goto :exit
)


Looking to create the directories listed in the set variables up top, using an iterative for loop that checks if the directories are there, and if not then it creates them.



Each variable step up localizes the proper target directory in another global batch file I am using to eventually distribute to other MDT servers, this is the section I'm working on now.




  • Edited the mistakes noted by Squashman @ 5:34EST 1/1/19 (Thanks dude)










share|improve this question




















  • 2





    What is this: &&a? Did you mean to do this: %%a? Regardless of that, you do not need to check for the existence of a directory before you try to create it. You can just create the directory and redirect the error to the nul device. You are also missing a closing parentheses for each of the FOR commands.

    – Squashman
    Jan 1 at 18:32













  • Yes. And yes, thank you. Create the directory and redirect the error to the nul device? Didn't think of that... I can read a lot of programming but when it comes to writing it, my scope is rather limited. I'm working on that though, thanks for the correction and tip!

    – Michael Cook
    Jan 1 at 22:17











  • basically... what is not listed in the script is a "what workstation do you want to use for DISM?" since the tool I use takes a bit of hardware/time to process and I don't want to use the server since it's a bit older. trying to use some pushd and popd but that didn't work. but your explanation might help. basically, the batch script has a lot of other sections that are all allowed through an elevation script. wish I was using a domain but it's all workgroup based at this time. Besides that though, because some of the commands get really long, the variables shorten a lot of the code.

    – Michael Cook
    Jan 1 at 22:46











  • be aware that pushd just sets the working folder, it does not push the workload (it's still the server that runs the script). You'd need something like PSEXEC to actually transfer the workload.

    – Stephan
    Jan 2 at 8:43











  • instead of psexec, i was thinking of using some powershell commands, or just xcopying the files and workload, still learning a bit of how to get things transferred back and forth from the script, although MDT is doing most of this work, i'm just writing this as a fallback script just in case. like LGPOs and such

    – Michael Cook
    Jan 3 at 18:01














0












0








0








I have some tools that I'm trying to batch script on the back end of an MDT server. One of those tools is DISM/ImageX. What I'm trying to automate is a script that checks for the folders so that DISM can extract the individual WIM files and then I can use another utility to slipstream updates.



Not into WSUS. Nor SCCM. I'm aware that it can be done with them, but I'm looking to keep my deployment times as close to 15-20 min each for the most up to date version of a given OS.



I have a lot of my back end tools working correctly, but I'm having a problem condensing this part of my batch file down so that it uses a for loop to check the directories instead of having the same code repeated over and over.



I never really got into batch scripting, but hey, it seems to do almost every trick I can think of and i'm always looking for ways to optimize my code.



Perhaps somebody might be able to see what I'm trying to do here with my batch file, tell me what I'm getting wrong?



I've written it out the long way but I want to condense this code down with =< 1 for loop.



setlocal
set EnabledDelayedExpansion
set _d=\cp
set 3d_=G:DISM
set 3d7a=%3d_%7
set 3d7b=%3d_%77.1
set 3d7c=%3d_%77.2
set 3d7d=%3d_%77.3
set 3d7e=%3d_%77.4
set 3d8a=%3d_%8
set 3d8b=%3d_%88.1
set 3d8c=%3d_%88.2
set 3d8d=%3d_%88.3
set 3d8e=%3d_%88.4
set 3dxa=%3d_%10
set 3dxb=%3d_%1010.1
set 3dxc=%3d_%1010.2
set 3dxd=%3d_%1010.3
set 3dxe=%3d_%1010.4
goto :3d7f

:3d7f
for /f "tokens=5 delims= " %%a in ('%3d7a% %3d7b% %3d7c% %3d7d% %3d7e%') do
(
if not exist %%a (
mkdir %%a)
goto :3d8f

:3d8f
for /f "tokens=5 delims= " %%a in ('%3d8a% %3d8b% %3d8c% %3d8d% %3d8e%') do
(
if not exist %%a (
mkdir %%a)
goto :3dxf

:3dxf
for /f "tokens=5 delims= " %%a in ('%3dxa% %3dxb% %3dxc% %3dxd% %3dxe%') do
(
if not exist %%a (
mkdir %%a)
goto :exit
)


Looking to create the directories listed in the set variables up top, using an iterative for loop that checks if the directories are there, and if not then it creates them.



Each variable step up localizes the proper target directory in another global batch file I am using to eventually distribute to other MDT servers, this is the section I'm working on now.




  • Edited the mistakes noted by Squashman @ 5:34EST 1/1/19 (Thanks dude)










share|improve this question
















I have some tools that I'm trying to batch script on the back end of an MDT server. One of those tools is DISM/ImageX. What I'm trying to automate is a script that checks for the folders so that DISM can extract the individual WIM files and then I can use another utility to slipstream updates.



Not into WSUS. Nor SCCM. I'm aware that it can be done with them, but I'm looking to keep my deployment times as close to 15-20 min each for the most up to date version of a given OS.



I have a lot of my back end tools working correctly, but I'm having a problem condensing this part of my batch file down so that it uses a for loop to check the directories instead of having the same code repeated over and over.



I never really got into batch scripting, but hey, it seems to do almost every trick I can think of and i'm always looking for ways to optimize my code.



Perhaps somebody might be able to see what I'm trying to do here with my batch file, tell me what I'm getting wrong?



I've written it out the long way but I want to condense this code down with =< 1 for loop.



setlocal
set EnabledDelayedExpansion
set _d=\cp
set 3d_=G:DISM
set 3d7a=%3d_%7
set 3d7b=%3d_%77.1
set 3d7c=%3d_%77.2
set 3d7d=%3d_%77.3
set 3d7e=%3d_%77.4
set 3d8a=%3d_%8
set 3d8b=%3d_%88.1
set 3d8c=%3d_%88.2
set 3d8d=%3d_%88.3
set 3d8e=%3d_%88.4
set 3dxa=%3d_%10
set 3dxb=%3d_%1010.1
set 3dxc=%3d_%1010.2
set 3dxd=%3d_%1010.3
set 3dxe=%3d_%1010.4
goto :3d7f

:3d7f
for /f "tokens=5 delims= " %%a in ('%3d7a% %3d7b% %3d7c% %3d7d% %3d7e%') do
(
if not exist %%a (
mkdir %%a)
goto :3d8f

:3d8f
for /f "tokens=5 delims= " %%a in ('%3d8a% %3d8b% %3d8c% %3d8d% %3d8e%') do
(
if not exist %%a (
mkdir %%a)
goto :3dxf

:3dxf
for /f "tokens=5 delims= " %%a in ('%3dxa% %3dxb% %3dxc% %3dxd% %3dxe%') do
(
if not exist %%a (
mkdir %%a)
goto :exit
)


Looking to create the directories listed in the set variables up top, using an iterative for loop that checks if the directories are there, and if not then it creates them.



Each variable step up localizes the proper target directory in another global batch file I am using to eventually distribute to other MDT servers, this is the section I'm working on now.




  • Edited the mistakes noted by Squashman @ 5:34EST 1/1/19 (Thanks dude)







loops batch-file for-loop dism






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 22:34







Michael Cook

















asked Jan 1 at 17:16









Michael CookMichael Cook

14




14








  • 2





    What is this: &&a? Did you mean to do this: %%a? Regardless of that, you do not need to check for the existence of a directory before you try to create it. You can just create the directory and redirect the error to the nul device. You are also missing a closing parentheses for each of the FOR commands.

    – Squashman
    Jan 1 at 18:32













  • Yes. And yes, thank you. Create the directory and redirect the error to the nul device? Didn't think of that... I can read a lot of programming but when it comes to writing it, my scope is rather limited. I'm working on that though, thanks for the correction and tip!

    – Michael Cook
    Jan 1 at 22:17











  • basically... what is not listed in the script is a "what workstation do you want to use for DISM?" since the tool I use takes a bit of hardware/time to process and I don't want to use the server since it's a bit older. trying to use some pushd and popd but that didn't work. but your explanation might help. basically, the batch script has a lot of other sections that are all allowed through an elevation script. wish I was using a domain but it's all workgroup based at this time. Besides that though, because some of the commands get really long, the variables shorten a lot of the code.

    – Michael Cook
    Jan 1 at 22:46











  • be aware that pushd just sets the working folder, it does not push the workload (it's still the server that runs the script). You'd need something like PSEXEC to actually transfer the workload.

    – Stephan
    Jan 2 at 8:43











  • instead of psexec, i was thinking of using some powershell commands, or just xcopying the files and workload, still learning a bit of how to get things transferred back and forth from the script, although MDT is doing most of this work, i'm just writing this as a fallback script just in case. like LGPOs and such

    – Michael Cook
    Jan 3 at 18:01














  • 2





    What is this: &&a? Did you mean to do this: %%a? Regardless of that, you do not need to check for the existence of a directory before you try to create it. You can just create the directory and redirect the error to the nul device. You are also missing a closing parentheses for each of the FOR commands.

    – Squashman
    Jan 1 at 18:32













  • Yes. And yes, thank you. Create the directory and redirect the error to the nul device? Didn't think of that... I can read a lot of programming but when it comes to writing it, my scope is rather limited. I'm working on that though, thanks for the correction and tip!

    – Michael Cook
    Jan 1 at 22:17











  • basically... what is not listed in the script is a "what workstation do you want to use for DISM?" since the tool I use takes a bit of hardware/time to process and I don't want to use the server since it's a bit older. trying to use some pushd and popd but that didn't work. but your explanation might help. basically, the batch script has a lot of other sections that are all allowed through an elevation script. wish I was using a domain but it's all workgroup based at this time. Besides that though, because some of the commands get really long, the variables shorten a lot of the code.

    – Michael Cook
    Jan 1 at 22:46











  • be aware that pushd just sets the working folder, it does not push the workload (it's still the server that runs the script). You'd need something like PSEXEC to actually transfer the workload.

    – Stephan
    Jan 2 at 8:43











  • instead of psexec, i was thinking of using some powershell commands, or just xcopying the files and workload, still learning a bit of how to get things transferred back and forth from the script, although MDT is doing most of this work, i'm just writing this as a fallback script just in case. like LGPOs and such

    – Michael Cook
    Jan 3 at 18:01








2




2





What is this: &&a? Did you mean to do this: %%a? Regardless of that, you do not need to check for the existence of a directory before you try to create it. You can just create the directory and redirect the error to the nul device. You are also missing a closing parentheses for each of the FOR commands.

– Squashman
Jan 1 at 18:32







What is this: &&a? Did you mean to do this: %%a? Regardless of that, you do not need to check for the existence of a directory before you try to create it. You can just create the directory and redirect the error to the nul device. You are also missing a closing parentheses for each of the FOR commands.

– Squashman
Jan 1 at 18:32















Yes. And yes, thank you. Create the directory and redirect the error to the nul device? Didn't think of that... I can read a lot of programming but when it comes to writing it, my scope is rather limited. I'm working on that though, thanks for the correction and tip!

– Michael Cook
Jan 1 at 22:17





Yes. And yes, thank you. Create the directory and redirect the error to the nul device? Didn't think of that... I can read a lot of programming but when it comes to writing it, my scope is rather limited. I'm working on that though, thanks for the correction and tip!

– Michael Cook
Jan 1 at 22:17













basically... what is not listed in the script is a "what workstation do you want to use for DISM?" since the tool I use takes a bit of hardware/time to process and I don't want to use the server since it's a bit older. trying to use some pushd and popd but that didn't work. but your explanation might help. basically, the batch script has a lot of other sections that are all allowed through an elevation script. wish I was using a domain but it's all workgroup based at this time. Besides that though, because some of the commands get really long, the variables shorten a lot of the code.

– Michael Cook
Jan 1 at 22:46





basically... what is not listed in the script is a "what workstation do you want to use for DISM?" since the tool I use takes a bit of hardware/time to process and I don't want to use the server since it's a bit older. trying to use some pushd and popd but that didn't work. but your explanation might help. basically, the batch script has a lot of other sections that are all allowed through an elevation script. wish I was using a domain but it's all workgroup based at this time. Besides that though, because some of the commands get really long, the variables shorten a lot of the code.

– Michael Cook
Jan 1 at 22:46













be aware that pushd just sets the working folder, it does not push the workload (it's still the server that runs the script). You'd need something like PSEXEC to actually transfer the workload.

– Stephan
Jan 2 at 8:43





be aware that pushd just sets the working folder, it does not push the workload (it's still the server that runs the script). You'd need something like PSEXEC to actually transfer the workload.

– Stephan
Jan 2 at 8:43













instead of psexec, i was thinking of using some powershell commands, or just xcopying the files and workload, still learning a bit of how to get things transferred back and forth from the script, although MDT is doing most of this work, i'm just writing this as a fallback script just in case. like LGPOs and such

– Michael Cook
Jan 3 at 18:01





instead of psexec, i was thinking of using some powershell commands, or just xcopying the files and workload, still learning a bit of how to get things transferred back and forth from the script, although MDT is doing most of this work, i'm just writing this as a fallback script just in case. like LGPOs and such

– Michael Cook
Jan 3 at 18:01












2 Answers
2






active

oldest

votes


















0














It looks like you might be trying to build a structure like IP addresses or similar. It's hard to tell with the strange variable naming. In any case, this should give you an idea of how to do it. This builds folders and subfolders from "0" to "255255255".



 setlocal enableDelayedExpansion
for /L %%a in (0,1,255) do (
set a=%%a
for /L %%b in (0,1,255) do (
set b=%%b
for /L %%c in (0,1,255) do (
set c=%%c
echo.!a!!b!!c!
mkdir !a!!b!!c! >nul 2>&1
)
)
)


If your objective uses some form of consistent structure then you should be able to use this as a basis to get it done.






share|improve this answer
























  • it's not IP addresses, but apparently Windows version numbers.

    – Stephan
    Jan 1 at 20:49











  • I understand that I could use what you've posted and see how it works & thank you. I'm just still unsure of how to write this logic myself. I don't really understand how to put variables into a token/delimiter list. Maybe I'm using the wrong tool in my head here, but what I wanted to do was use a list that refers to variables that are set elsewhere. If I start with a list of options that determine what server I'm accessing, then all of the sub variables are dependent on that selection. I'm looking to see how I'd be able to use a list with specific tokens, but maybe your suggestion's easier.

    – Michael Cook
    Jan 1 at 22:30



















0














You don't need to set so many variables (none at all to be precise).

As Squashman already commented, &&a should be %%a,

you're missing closing parentheses and

there is no need to check the folder for existence (just redirect the error message to NUL).

Also your for loops are wrong. You don't want to use the fifth token only and you don't want to execute the variable list ('the single quotes' does that).



You need just plain for loops. The following code has the same outcome, but it's much easier and shorter:



for %%a in (7 8 10) do (
for /l %%b in (1,1,4) do (
ECHO Debug: md "G:DISM%%a%%a.%%b"
md "G:DISM%%a%%a.%%b" >nul
)
)


Instead of the for /l you can also do for %%b in (1 2 3 4) do (






share|improve this answer
























  • well, the variables point to a global batch file, this script doesn't show it, but I set the \cp and G: to static variables so that I could refine the script. not entirely sure that I need that many? but the variables are for DISM to process and imagex to export to network folders.

    – Michael Cook
    Jan 1 at 22:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997420%2funderstanding-batch-for-loops-with-in-variable1-variable2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














It looks like you might be trying to build a structure like IP addresses or similar. It's hard to tell with the strange variable naming. In any case, this should give you an idea of how to do it. This builds folders and subfolders from "0" to "255255255".



 setlocal enableDelayedExpansion
for /L %%a in (0,1,255) do (
set a=%%a
for /L %%b in (0,1,255) do (
set b=%%b
for /L %%c in (0,1,255) do (
set c=%%c
echo.!a!!b!!c!
mkdir !a!!b!!c! >nul 2>&1
)
)
)


If your objective uses some form of consistent structure then you should be able to use this as a basis to get it done.






share|improve this answer
























  • it's not IP addresses, but apparently Windows version numbers.

    – Stephan
    Jan 1 at 20:49











  • I understand that I could use what you've posted and see how it works & thank you. I'm just still unsure of how to write this logic myself. I don't really understand how to put variables into a token/delimiter list. Maybe I'm using the wrong tool in my head here, but what I wanted to do was use a list that refers to variables that are set elsewhere. If I start with a list of options that determine what server I'm accessing, then all of the sub variables are dependent on that selection. I'm looking to see how I'd be able to use a list with specific tokens, but maybe your suggestion's easier.

    – Michael Cook
    Jan 1 at 22:30
















0














It looks like you might be trying to build a structure like IP addresses or similar. It's hard to tell with the strange variable naming. In any case, this should give you an idea of how to do it. This builds folders and subfolders from "0" to "255255255".



 setlocal enableDelayedExpansion
for /L %%a in (0,1,255) do (
set a=%%a
for /L %%b in (0,1,255) do (
set b=%%b
for /L %%c in (0,1,255) do (
set c=%%c
echo.!a!!b!!c!
mkdir !a!!b!!c! >nul 2>&1
)
)
)


If your objective uses some form of consistent structure then you should be able to use this as a basis to get it done.






share|improve this answer
























  • it's not IP addresses, but apparently Windows version numbers.

    – Stephan
    Jan 1 at 20:49











  • I understand that I could use what you've posted and see how it works & thank you. I'm just still unsure of how to write this logic myself. I don't really understand how to put variables into a token/delimiter list. Maybe I'm using the wrong tool in my head here, but what I wanted to do was use a list that refers to variables that are set elsewhere. If I start with a list of options that determine what server I'm accessing, then all of the sub variables are dependent on that selection. I'm looking to see how I'd be able to use a list with specific tokens, but maybe your suggestion's easier.

    – Michael Cook
    Jan 1 at 22:30














0












0








0







It looks like you might be trying to build a structure like IP addresses or similar. It's hard to tell with the strange variable naming. In any case, this should give you an idea of how to do it. This builds folders and subfolders from "0" to "255255255".



 setlocal enableDelayedExpansion
for /L %%a in (0,1,255) do (
set a=%%a
for /L %%b in (0,1,255) do (
set b=%%b
for /L %%c in (0,1,255) do (
set c=%%c
echo.!a!!b!!c!
mkdir !a!!b!!c! >nul 2>&1
)
)
)


If your objective uses some form of consistent structure then you should be able to use this as a basis to get it done.






share|improve this answer













It looks like you might be trying to build a structure like IP addresses or similar. It's hard to tell with the strange variable naming. In any case, this should give you an idea of how to do it. This builds folders and subfolders from "0" to "255255255".



 setlocal enableDelayedExpansion
for /L %%a in (0,1,255) do (
set a=%%a
for /L %%b in (0,1,255) do (
set b=%%b
for /L %%c in (0,1,255) do (
set c=%%c
echo.!a!!b!!c!
mkdir !a!!b!!c! >nul 2>&1
)
)
)


If your objective uses some form of consistent structure then you should be able to use this as a basis to get it done.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 19:51









shawnshawn

26615




26615













  • it's not IP addresses, but apparently Windows version numbers.

    – Stephan
    Jan 1 at 20:49











  • I understand that I could use what you've posted and see how it works & thank you. I'm just still unsure of how to write this logic myself. I don't really understand how to put variables into a token/delimiter list. Maybe I'm using the wrong tool in my head here, but what I wanted to do was use a list that refers to variables that are set elsewhere. If I start with a list of options that determine what server I'm accessing, then all of the sub variables are dependent on that selection. I'm looking to see how I'd be able to use a list with specific tokens, but maybe your suggestion's easier.

    – Michael Cook
    Jan 1 at 22:30



















  • it's not IP addresses, but apparently Windows version numbers.

    – Stephan
    Jan 1 at 20:49











  • I understand that I could use what you've posted and see how it works & thank you. I'm just still unsure of how to write this logic myself. I don't really understand how to put variables into a token/delimiter list. Maybe I'm using the wrong tool in my head here, but what I wanted to do was use a list that refers to variables that are set elsewhere. If I start with a list of options that determine what server I'm accessing, then all of the sub variables are dependent on that selection. I'm looking to see how I'd be able to use a list with specific tokens, but maybe your suggestion's easier.

    – Michael Cook
    Jan 1 at 22:30

















it's not IP addresses, but apparently Windows version numbers.

– Stephan
Jan 1 at 20:49





it's not IP addresses, but apparently Windows version numbers.

– Stephan
Jan 1 at 20:49













I understand that I could use what you've posted and see how it works & thank you. I'm just still unsure of how to write this logic myself. I don't really understand how to put variables into a token/delimiter list. Maybe I'm using the wrong tool in my head here, but what I wanted to do was use a list that refers to variables that are set elsewhere. If I start with a list of options that determine what server I'm accessing, then all of the sub variables are dependent on that selection. I'm looking to see how I'd be able to use a list with specific tokens, but maybe your suggestion's easier.

– Michael Cook
Jan 1 at 22:30





I understand that I could use what you've posted and see how it works & thank you. I'm just still unsure of how to write this logic myself. I don't really understand how to put variables into a token/delimiter list. Maybe I'm using the wrong tool in my head here, but what I wanted to do was use a list that refers to variables that are set elsewhere. If I start with a list of options that determine what server I'm accessing, then all of the sub variables are dependent on that selection. I'm looking to see how I'd be able to use a list with specific tokens, but maybe your suggestion's easier.

– Michael Cook
Jan 1 at 22:30













0














You don't need to set so many variables (none at all to be precise).

As Squashman already commented, &&a should be %%a,

you're missing closing parentheses and

there is no need to check the folder for existence (just redirect the error message to NUL).

Also your for loops are wrong. You don't want to use the fifth token only and you don't want to execute the variable list ('the single quotes' does that).



You need just plain for loops. The following code has the same outcome, but it's much easier and shorter:



for %%a in (7 8 10) do (
for /l %%b in (1,1,4) do (
ECHO Debug: md "G:DISM%%a%%a.%%b"
md "G:DISM%%a%%a.%%b" >nul
)
)


Instead of the for /l you can also do for %%b in (1 2 3 4) do (






share|improve this answer
























  • well, the variables point to a global batch file, this script doesn't show it, but I set the \cp and G: to static variables so that I could refine the script. not entirely sure that I need that many? but the variables are for DISM to process and imagex to export to network folders.

    – Michael Cook
    Jan 1 at 22:40
















0














You don't need to set so many variables (none at all to be precise).

As Squashman already commented, &&a should be %%a,

you're missing closing parentheses and

there is no need to check the folder for existence (just redirect the error message to NUL).

Also your for loops are wrong. You don't want to use the fifth token only and you don't want to execute the variable list ('the single quotes' does that).



You need just plain for loops. The following code has the same outcome, but it's much easier and shorter:



for %%a in (7 8 10) do (
for /l %%b in (1,1,4) do (
ECHO Debug: md "G:DISM%%a%%a.%%b"
md "G:DISM%%a%%a.%%b" >nul
)
)


Instead of the for /l you can also do for %%b in (1 2 3 4) do (






share|improve this answer
























  • well, the variables point to a global batch file, this script doesn't show it, but I set the \cp and G: to static variables so that I could refine the script. not entirely sure that I need that many? but the variables are for DISM to process and imagex to export to network folders.

    – Michael Cook
    Jan 1 at 22:40














0












0








0







You don't need to set so many variables (none at all to be precise).

As Squashman already commented, &&a should be %%a,

you're missing closing parentheses and

there is no need to check the folder for existence (just redirect the error message to NUL).

Also your for loops are wrong. You don't want to use the fifth token only and you don't want to execute the variable list ('the single quotes' does that).



You need just plain for loops. The following code has the same outcome, but it's much easier and shorter:



for %%a in (7 8 10) do (
for /l %%b in (1,1,4) do (
ECHO Debug: md "G:DISM%%a%%a.%%b"
md "G:DISM%%a%%a.%%b" >nul
)
)


Instead of the for /l you can also do for %%b in (1 2 3 4) do (






share|improve this answer













You don't need to set so many variables (none at all to be precise).

As Squashman already commented, &&a should be %%a,

you're missing closing parentheses and

there is no need to check the folder for existence (just redirect the error message to NUL).

Also your for loops are wrong. You don't want to use the fifth token only and you don't want to execute the variable list ('the single quotes' does that).



You need just plain for loops. The following code has the same outcome, but it's much easier and shorter:



for %%a in (7 8 10) do (
for /l %%b in (1,1,4) do (
ECHO Debug: md "G:DISM%%a%%a.%%b"
md "G:DISM%%a%%a.%%b" >nul
)
)


Instead of the for /l you can also do for %%b in (1 2 3 4) do (







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 21:06









StephanStephan

35.9k43457




35.9k43457













  • well, the variables point to a global batch file, this script doesn't show it, but I set the \cp and G: to static variables so that I could refine the script. not entirely sure that I need that many? but the variables are for DISM to process and imagex to export to network folders.

    – Michael Cook
    Jan 1 at 22:40



















  • well, the variables point to a global batch file, this script doesn't show it, but I set the \cp and G: to static variables so that I could refine the script. not entirely sure that I need that many? but the variables are for DISM to process and imagex to export to network folders.

    – Michael Cook
    Jan 1 at 22:40

















well, the variables point to a global batch file, this script doesn't show it, but I set the \cp and G: to static variables so that I could refine the script. not entirely sure that I need that many? but the variables are for DISM to process and imagex to export to network folders.

– Michael Cook
Jan 1 at 22:40





well, the variables point to a global batch file, this script doesn't show it, but I set the \cp and G: to static variables so that I could refine the script. not entirely sure that I need that many? but the variables are for DISM to process and imagex to export to network folders.

– Michael Cook
Jan 1 at 22:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997420%2funderstanding-batch-for-loops-with-in-variable1-variable2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith