![]() ![]() C:\>set a=5ītw, If you had cmd /v:on or in a batch file, EnableDelayedExpansion on, then as Dave shows, you'd have to be aware of ! being a special character so you'd have an issue if the ! was within a %var%. v:off even experts, (perhaps because other things can be interpreted differnly with it), so i'm just using this to show you that you can try the !var! notation within cmd with it. ![]() Starting with the default, cmd /v:off then moving to cmd/v:on Most use cmd. You can also test the notation from the command line with If "!VAR!" = "after" If you see this, it worked If delayed variable expansion is enabled, the aboveĮxamples could be written as follows to work as intended: set VAR=before ![]() Which just keeps setting LIST to the last file found.ĭelayed environment variable expansion allows you to use a differentĬharacter (the exclamation mark) to expand environment variables atĮxecution time. So theĪctual FOR loop we are executing is: for %i in (*) do set LIST= %i Statement is read, and at that time the LIST variable is empty. In that it will NOT build up a list of files in the current directory,īut instead will just set the LIST variable to the last file found.Īgain, this is because the %LIST% is expanded just once when the FOR Inside the compound statement is really comparing "before" with Includes the body of the IF, which is a compound statement. Is substituted when the first IF statement is read, since it logically Would never display the message, since the %VAR% in BOTH IF statements If "%VAR%" = "after" If you see this, it worked The following exampleĭemonstrates the problem with immediate variable expansion: set VAR=before The limitations of the current expansion which happens when a line of See CMDĭelayed environment variable expansion is useful for getting around This support is always disabled by default, but may beĮnabled/disabled via the /V command line switch to CMD.EXE. You have to use !var! and to get !var! to work you have to setlocal EnableDelayedExpansionĪn explanation of this exists in cmd help, though it's not obvious which command's help explains that! It's set /?įinally, support for delayed environment variable expansion has beenĪdded. offĭoing echo %var% within a block like FOR or IF, doesn't work correctly. The speed is not important if executed a few times, but it becomes very significant if executed thousands of times in a loop. You could also get the desired result using CALL with double percents, but this is much slower. This can be avoided by additional SETLOCAL: for /D %%G in (*) do ( Note that delayed expansion occurs after FOR variable expansion, so the result will be corrupted if %%G constains !. The following code using delayed expansion will give the result you seek: off Note that delayed expansion requires delayed expansion to be enabled via SETLOCAL EnableDelayedExpansion Most parsing has completed by now -Ģ) FOR variable expansion: for %%A in (*) do echo %%Aģ) Delayed environment variable expansion: echo !var!Ĥ) CALL % expansion - Parameter: call echo %%1 or Environment variable: call echo %%var%%ĥ) SET /A environment variable expansion: `set /a "value=var+1" It is all explained in the link, but in summary, the order of expansion is:ġ) % expansion - Parameter: echo %1 or Environment variable: echo %var% There are multiple points where various types of variables are expanded, and you must fully understand them if you truly want to make the most of batch programming. The CMD processor is a complicated beast (and also poorly documented). Your logic is wrong - the assignment of _newp should be based on the value of _srcp ![]() The solution is delayed expansion, which occurs as each command within the loop is being executed. So the value is the value that existed before the loop was started. %var% expansion occurs when the statement is parsed, and the entire parenthesized code block is parsed in one pass, before any commands are executed. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |