In visual basic programming, you can use subroutines to perform different operations many times, at different times, and to pass arguments to be used in those subs. In batch, this is a little bit different.
Calling a sub is quite easy. It's just like the GOTO command!
Syntax:
In calling a sub, parameters (%1, %2, %3, etc.) are optional, just like a regular batch file, but can be useful (also like a regular batch file). If the batch file already has parameters and you call a sub, the subroutine's parameters will be used instead of the batch file's until the subroutine has completed.
Here is an example of the proper usage of subs with parameters being passed.
Sub-Example.bat
Subs work on a simple concept: The code is preformed from the subroutine's beginning until it reaches the end of the file by either being sent there with a GOTO command (goto :eof), or it physically reaches the end of the file, then the script continues on at the next command after the call command.
Now, that simple script just says Step 1, Step 2, Step 3, doesn't save very many lines. In fact, I can do it in 5 lines (not counting @echo off) and it's probably possible in less.
Pseudo-Sub-Example.bat
That makes batch subroutines seem pretty inefficient, right? Well, in that sort of situation, I would agree with you. However, if you have multiple parts of your script that would be preforming the EXACT SAME CODE at different points in time, then it can be very useful.
For example: Subs.bat
In that script, it checks if .doc files exist, and if they do, delete them all. Now, if subroutines didn't exist (or you were just using regular GOTOs), it would be pretty hard to get the same spot you left off. Here is an example of the same code, except reworked so it does the exact same thing minus the call commands:
Pseudo-Subs.bat
Now, that's a difference of 5 lines. But the line difference will increase by 2 each time you go to the subroutine. That can get pretty annoying, right? For each time you want to go to :checker, you have to place another label and if statement. If you're doing it 50+ times, it can be really tedious (doing an extra 100 lines of code).
Now does it seem so inefficient?
Calling a sub is quite easy. It's just like the GOTO command!
Syntax:
- Code:
Call :labelname [Parameters]
In calling a sub, parameters (%1, %2, %3, etc.) are optional, just like a regular batch file, but can be useful (also like a regular batch file). If the batch file already has parameters and you call a sub, the subroutine's parameters will be used instead of the batch file's until the subroutine has completed.
Here is an example of the proper usage of subs with parameters being passed.
Sub-Example.bat
- Code:
@echo off
call :label 1
Echo Step 2
call :label 3
pause
goto :eof
:label
Echo Step %1
Subs work on a simple concept: The code is preformed from the subroutine's beginning until it reaches the end of the file by either being sent there with a GOTO command (goto :eof), or it physically reaches the end of the file, then the script continues on at the next command after the call command.
Now, that simple script just says Step 1, Step 2, Step 3, doesn't save very many lines. In fact, I can do it in 5 lines (not counting @echo off) and it's probably possible in less.
Pseudo-Sub-Example.bat
- Code:
@echo off
:loop
set /a step+=1
Echo Step %step%
if %step%==3 pause & exit
goto loop
That makes batch subroutines seem pretty inefficient, right? Well, in that sort of situation, I would agree with you. However, if you have multiple parts of your script that would be preforming the EXACT SAME CODE at different points in time, then it can be very useful.
For example: Subs.bat
- Code:
@echo off
Rem This will check if a .doc file exists periodically and delete them if they exist.
Echo I like .txt files, but I hate .doc files.
Call :checker
Echo Let's do some simple math...
set /p ans=1+1^=?
if "%ans%"=="2" (echo correct) else (echo YOU FAIL!)
Call :checker
pause > nul
goto :eof
:checker
dir /b *.doc 2> nul
If %errorlevel%==1 (Echo YAY!) else (Del *.doc & echo YAY!)
In that script, it checks if .doc files exist, and if they do, delete them all. Now, if subroutines didn't exist (or you were just using regular GOTOs), it would be pretty hard to get the same spot you left off. Here is an example of the same code, except reworked so it does the exact same thing minus the call commands:
Pseudo-Subs.bat
- Code:
@echo off
Rem This will check if a .doc file exists periodically and delete them if they exist.
Echo I like .txt files, but I hate .doc files.
Goto :checker
:First
Echo Let's do some simple math...
set /p ans=1+1^=?
if "%ans%"=="2" (echo correct) else (echo YOU FAIL!)
Goto :checker
:Second
pause > nul
goto :eof
:checker
set /a count+=1
dir /b *.doc 2> nul
If %errorlevel%==1 (Echo YAY!) else (Del *.doc & echo YAY!)
If %count%==1 goto First
If %count%==2 goto Second
Now, that's a difference of 5 lines. But the line difference will increase by 2 each time you go to the subroutine. That can get pretty annoying, right? For each time you want to go to :checker, you have to place another label and if statement. If you're doing it 50+ times, it can be really tedious (doing an extra 100 lines of code).
Now does it seem so inefficient?
- Spoiler:
- That was a rhetorical question...Don't answer it!