Using functions in cmd (advanced scripting no.3)
Comment from Alun Jones (thx!) reminds me I forget to mention "functions" in batches! We were talking about script block, which are great if you combine them with call... Lets have a look what I have in my mind:
Echo Beginning...
...
...
Call :CheckForUserName %UserName%
...
...
Echo End...
Goto EndOfScript
:: Subroutines
:CheckForUserName
Net User %1 /Domain > nul
If %ErrorLevel% NEQ 0 (
Echo User doesnt exist
Goto EndOfScript
) Else (
Echo User exist
)
:EndOfScript
If you use it this way, you can see that it will execute CheckForUserName with parametere username and then it will return back (where Call was used), which can be extremely usefull sometimes.
However sometimes you need to end function (for example if error is encountered). Many people are asking me why I am always using EndOfScript when I can simply use Goto :EOF command - the reason is that if Goto :EOF is used inside this function, it WONT just to end of script - it will simply jump to end of function!