Read Mode
Default

WTF Difference between Variable - %VAR% & !VAR! | Batch Basics

Question asked about batch programming
Question!
Well that's An Awesome Question! As Many of the batch programmers seems to be using the two different forms of variables syntax without knowing the real difference between them. I also don't know all the differences, And Benefits of using Separate Syntax - but based on my experience with batch programs. I think that I can specify few of them. If you Don't Want to read the Whole explanation here, You can Download the Example files form the Download Link at the end of this Article.

I've always believed in the fact that, Learning  anything via examples is much easier than reading about it. Thus, We always provide example files in the Download link at the end. As you are still reading, So lets talk about the Answer to the asked question By Mr. Surya.

As the usage of single % sign around the name of the variable, i.e. %Variable_name% is the standard form of representing the variable in Batch programming. while using ! around the Name of variable is a special type of variable syntax, which I often called the Delayed Expansion Condition [DEC] variable.

Related Post: The Stopwatch | By Catur Surya

But in some cases like looping, using the for loop in any batch program... If you use the standard form of variable Syntax (%var%), then it seems that the for loop is running so fast that the change in the value of variable didn't seems to take place. Or In other words... the value inside the variable remains same even after running the complete for loopSo, we have to add an extra line before Using the DEC variable... which is 

Setlocal EnableDelayedExpansion

Which means we have to make all variables Local & Enable the Delayed expansion of For loop, so changes made on our variable values can easily take place. Now, after adding the above line on top of your Batch Program. You can easily use the DEC variable in your batch program.

I know it'll be Difficult for you to understand what I'm talking about. But, It'll be easier to understand the difference with some examples, Rather than with paragraphs. So, here in this example - I have considered a Scenario, When you need to Print First 10 natural Numbers on the CMD by Using the FOR Loop. Lets have a Closer Look!

WTF Difference between Variable - %VAR% & !VAR! | Batch Basics
Example_1
Problem With Standard syntax [%var%] of variable inside FOR Loop, is As shown in the above screenshot of CMD. As you can notice that, we have incremented the variable value each time in the loop - But, It seems like that FOR LOOP is running so fast that the increment of the varibale is not happening. So, It's printing out '1' each time. Although, After the complete execution of the LOOP - You can notice the value of the variable is incremented to '2'. And My reaction when, I first came across this problem was like...



This Small lack of knowledge about DEC has ruined 2 months of my life. (Summer holidays) When I was trying to establishing a project using Batch. But, this little problem resulted in unwanted output of the project. And, Finally I have to drop that project and start with the new approach. But, At last - I successfully completed it. So, Here after Fixing this little problem with the Fast execution of FOR LOOP. I've Got the following Output.

WTF Difference between Variable - %VAR% & !VAR! | Batch Basics
Example_2
Hence, I can Assume that - You've understand the concept of DEC variable. It is nothing Special but, Needed when you want to do calculations inside the FOR LOOP. Hope, you'll like my explanation and Article. You can simply Download the Example files from the Links Given Below. Open the batch code with any text editor, And Read the comments given with the corresponding lines... ☺

"No one is dumb, who is curious.
The people who don't ask questions remain clueless throughout their lives."
If You are Still in Doubt or Want to tell any of your experience with this DEC Variable. You can simply Comment about it below. We'll try to explain it or Add it in the article too. Thanks For your Time. 

Keep Sharing, Keep Learning...
Be Happy, Spread Happiness...

Download Link:

No comments:

Powered by Blogger.