I always try to avoid writing Batch script files as it's really tedious to maintain them in contrary of other scripting languages like BASH for example.
But sometimes it's unavoidable to deal with some Batch code. That's why it's really important to understand some concepts specially the "Delayed Expansion" concept in batch files.
This is a good tutorial of understanding the concept of "Delayed Expansion" in batch files. This is a good concept to know when writing a Batch file that is including relatively not simple code (for example if, for, ..etc) statements.