Batch files make you to use 'goto', an obsolete and quite unpleasant construction that makes your code unreadable. This is a common opinion, but I want to discuss the way to avoid 'goto' commands in your batch scripts.
The traditional way to build batch files since the early days of MS-DOS looks like this:
if condition goto :THEN
rem Something like ‘else’
rem ...
goto IF_END
:THEN
rem Something like ‘then’
rem ...
:IF_END
This is really different from the way of writing 'if' statements in C, Pascal, Java and even VBScript. Look at the modern batch script below. Does it look like C or Java code?
if condition (
rem ‘then’
rem ...
) else (
rem ‘else’
rem ...
)
It's not hard to use these constructions, simply don't forget to add 'setlocal enabledelayedexpansion' (without commas).
if condition goto :THEN
rem Something like ‘else’
rem ...
goto IF_END
:THEN
rem Something like ‘then’
rem ...
:IF_END
This is really different from the way of writing 'if' statements in C, Pascal, Java and even VBScript. Look at the modern batch script below. Does it look like C or Java code?
if condition (
rem ‘then’
rem ...
) else (
rem ‘else’
rem ...
)
It's not hard to use these constructions, simply don't forget to add 'setlocal enabledelayedexpansion' (without commas).
No comments:
Post a Comment