Is there a way to run more than one command on the same line where the first command is a "for loop" in a windows bat file?
For example run these three commands on the same line:
for /f %%i in ('echo bar') do set result=%%i
set foo=%result%
echo %foo%
// Displays: bar
Below example didn't originally work but works now after adding delayed expansion per the suggestion in the comments (for future reference):
setlocal enabledelayedexpansion
(for /f %%i in ('echo bar') do set result=%%i) & set foo=%result% & echo %foo%
So by putting parenthesis around the for loop related commands and using delayedexpansion, more commands can be added with the ampersand separator.
Replacing the for loop with a simpler command also works with delayedexpansion:
setlocal enabledelayedexpansion
set result=bar & set foo=%result% & echo %foo%
// displays: bar
So per the comments all of the above now work, the last two examples by adding setlocal enabledelayedexpansion.
Thanks all.
PS: I don't see this is a duplicate question per aschipfl since my question was how to get the three commands to work on one line where one of the commands is more complex (a for loop with its own command(s)). There's no way to find that answer from the question indicated via a search unless one knows in advance to search for delayedexpansion, in which case the question wouldn't be neccessary since that's the answer. The answers may be the same, but the questions are different.