Guest Posted May 23, 2014 Share Posted May 23, 2014 (edited) But it should make a difference, jaclaz. Using % instead of ! gives only the initial value for the variable, not the most recent one. Edited May 23, 2014 by 5eraph Link to comment Share on other sites More sharing options...
bphlpt Posted May 23, 2014 Share Posted May 23, 2014 But it should make a difference, jaclaz. Using % instead of ! gives only the initial value for the variable, not the most recent one. You would be correct 5eraph, if the variable usage had been inside a FOR loop, but not if it is just consecutive statements, unless I'm not understanding what you mean. In other words:@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONSET "_OSV=1"SET "_OSV=2"SET "_OSV=3"SET "_OSV=4"SET "_OSV=5"SET "_OSV=6"SET "_OSV=7"SET "_OSV=8"SET "_OSV=9"SET "_OSV=0"SET "_OSV=" & FOR /f "TOKENS=2 DELIMS=[]" %%A IN ('VER') DO FOR /f "TOKENS=2,3 DELIMS=. " %%B IN ("%%~A") DO SET "_OSV=%%B.%%C"ECHO= _OSV=%_OSV%ECHO= _OSV=!_OSV!will output two lines of the same thing -- _OSV=5.1 (or whatever the value is for your OS)Cheers and Regards Link to comment Share on other sites More sharing options...
Guest Posted May 23, 2014 Share Posted May 23, 2014 You're right. Seems counter to what my source describes. Link to comment Share on other sites More sharing options...
jaclaz Posted May 23, 2014 Share Posted May 23, 2014 Yep, that's the idea behind delayed expansion. (though the name is IMHO counterintuitive, as with delayed expansion on and ! you get the "current" value, whilst with % you get the value it had "before" entering the FOR loop). See if this other source is more clear:http://www.robvanderwoude.com/variableexpansion.php jaclaz Link to comment Share on other sites More sharing options...
bphlpt Posted May 23, 2014 Share Posted May 23, 2014 (edited) @5eraph, I absolutely love ss64.com, and robvanderwoude.com as well, for batch reference, but you probably misinterpreted the two special case examples given the first thing on your ss64 source page. I might not be wording this completely right, but I'll try. The key to remember is that without delayed expansion enabled, and/or variables using %, each batch statement is parsed and evaluated in its entirety with the value of the % variables as they were just before the statement was executed. In the first example:Set _var=firstSet _var=second& Echo %_var% !_var!The second line, and any other time that multiple statements are connected with & or || or &&, is considered to be ONE batch statement which is why %_var% and !_var! will have different values when delayed expansion is enabled. As jaclaz said, ' with delayed expansion on and ! you get the "current" value, whilst with % you get the value it had "before" '. The same is true with a FOR loop, or IF THEN, or any other collection of statements enclosed in parentheses, regardless if it is multi-line or all on one line. That is one batch statement, no matter how many lines are involved, that is parsed completely before it is executed.The second example is even trickier, but it basically is a way of escaping a redirection, and other characters that would normally "screw up" a batch command by trying to cause an action, to be able to use batch to work with HTML and XML statements. You can find another explanation of EnableDelayedExpansion here - http://blogs.msdn.com/b/oldnewthing/archive/2006/08/23/714650.aspxHope that helps.Cheers and Regards Edited May 24, 2014 by bphlpt Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now