Creating subroutines in batch files is easy. Just create a label, a goto :EOF and call :label. However,
if the subroutine uses a setlocal-endlocal
block you might want to be able to pass variable changes out of that block and
still retain the local scope to avoid pollution of the upper scope.
You can exploit the fact that cmd expands variables on reading a line before actually executing it:
The trick here is that variable expansion takes place before endlocal (or the set following it) is executed and set is executed after endlocal and thus affects the outer scope, but
is able to pass a value of the inner scope along.
Note: I've seen this trick before at Paul Sadowski's site and found it worth remembering, although I didn't use it that much so far (my Bignum implementation will, however make extensive use if it).
Comments
Post new comment