Monday, November 27, 2006 8:51 AM
bart
Answers to quiz - Batch Scripting Mysteries
A few days ago, I posted this little quiz on batch scripting mysteries. The batch script given was:
@echo off
for %%f in (a,b,c) do (
echo 1 %%f 2
set x=x1 %%f x2
echo %x%
)
Here's the output:
C:\temp>test
1 a 2
ECHO is off.
1 b 2
ECHO is off.
1 c 2
ECHO is off.
C:\temp>test
1 a 2
x1 c x2
1 b 2
x1 c x2
1 c 2
x1 c x2
Not what you might have expected :-). I did some research but HELP SET didn't give the complete answer, so a workaround seems a good solution:
@echo off
for %%f in (a,b,c) do (
echo 1 %%f 2
call :print %%f
)
goto eof
:print
set x=x1 %1 x2
echo %x%
:eof
And of course Windows PowerShell helps us out too:
PS C:\Users\Bart> ('a','b','c') | foreach { echo "1 $_ 2"; $x = "x1 $_ x2"; echo $x }
1 a 2
x1 a x2
1 b 2
x1 b x2
1 c 2
x1 c x2
Yet another reason to choose Windows PowerShell? (Notice the variable expansion in strings too.)
Del.icio.us |
Digg It |
Technorati |
Blinklist |
Furl |
reddit |
DotNetKicks
Filed under: Windows PowerShell