Can someone give me an example of where a batch script would act differently with or without delayed expansion? Are there any situations where you would NOT want to use delayed expansion? Thanks.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Look at the following examples…
Example 1: The following code DOESN’T use delayed expansion, so the variables in the for loop are expanded only one time. This means that
%Count%will always expand to0in each iteration of the loop, no matter what we do to it with the set command:So this script will output:
This is not how this loop is supposed to work.
Example 2: On the other hand, if we use delayed expansion, we have the following script, which will run as expected.
and, as expected, it will output:
When you use the
ENABLEDELAYEDEXPANSION, and expand a variable using!instead of%, the variable is re-expanded each time, and everything works as it’s supposed to.