0

Why does the following successfully echo %vol% whereas if I delete the goto done it will not echo %vol%?

@echo off
setlocal DisableDelayedExpansion
pushd d:
for /f "tokens=1-5*" %%1 in ('vol') do ( 
set vol=%%6 & goto done
)
:done
echo.%vol%
pause
EXIT /b

This is a question similar to, but not the same as DOS batch: SET variable and ECHO it within (...) block

Even without the goto done %vol% survives the end of the block since I can use it in comparisons, and even echo it using enabled expansion. I'm curious why just the echo command seems to be crippled with disabled expansion.

RKO
  • 161
  • 10
  • 1
    Because `vol` produces _two_ non-empty lines. The 6th token of the 1st line is volume name while the 2nd line has only 5 tokens and its 6th token is empty… – JosefZ Jul 03 '20 at 19:23
  • Could you further explain how this relates to my question of having to use the goto done? Without goto done I get the empty response, but with the goto done I get the correct volume name. – RKO Jul 03 '20 at 19:43
  • 2
    `goto done` effectively breaks out of the `for` loop. Without it, the loop continues to the next line and the next until all the lines are looped through. The `vol` variable is set to the new value each time through the loop. Once you're done with the loop, the final value that it was set to is its value. – avery_larry Jul 03 '20 at 19:46
  • 2
    Side note -- I would not recommend using %%1 as the `for` token since it is easily confused with `%1`. – avery_larry Jul 03 '20 at 19:49
  • Run the `VOL` command from a command prompt. – Squashman Jul 03 '20 at 20:27
  • Thanks for the replies, but I'm still not seeing the rationale. Without the goto done I am still exiting the loop and %vol% is (i repeat "is") set to the correct value. The only thing not working is echo. – RKO Jul 04 '20 at 15:53

0 Answers0