Tuesday, January 22, 2013

Some “fun” hacks with batch script

  1. Always quote variable or value in “IF” statements.

    This script will return expected results if both var1 and var2 has some value and don't have any spaces in the value

    So, what is the fix here. You can either put quote around the value or variable. If you are checking an environment variable that holds file path like JAVA_HOME, you may not able to put quote around the value. You have to follow the later approach.

  2. Inside an “IF” block, if you want to set some value to a variable and that value contains parentheses, quote the SET statement like below.

    Following script will fail, because it has parentheses and it gets mixed up with IF statement syntax.

    If you can put quote around the value, wrap the value with quotes.

    If you can't put quote around the value, put quote around entire statement excluding SET

0 Comments :