Batch to the Roots! Page 2
GOTO
One of the biggest disadvantages in Batch has always been the absence of subroutines. The only way to solve this problem is to create seperate batchfiles and call these as if they were subroutines or trying to keep all the commands on one line. By extending the CALL and GOTO function Batch has been enhanced to accommodate subroutines as is shown by the following example:
@echo off :sub This script searches the WINNT directory for files without a .EXE extension. The only other way to accomplish a script
like this would have been to add the commands on one line with the '|' seperator. This usually creates messy scripts.
An Example
Let's create a script which demonstrates some of the new functionality together.
Suppose we want to do a quick scan of our Windows NT network to check for the existence of machines
with a SYMBIOS SCSI interface. @echo off :getstation :checkfile
The above script executes our desired action. First we use the command 'net view' to get a list of
active stations in our domain. Because the net view command produces some extra data we filter the output
to get only lines starting with '\\' and only the first column of data (to get rid of any remarks which might be present after the computername).
As you can see Batch isn't dead yet and still can have uses where it outperforms
more complicated languages.
FOR %%i IN ("c:\winnt\*.*") DO call :sub %%i
goto
:eof
if /I "%~x1" NEQ ".EXE" echo %~f1
goto :eof
A simple way to do this would be to check for the existence of the file SYMC8xx.SYS
FOR /F "usebackq tokens=1" %%i IN (`net view`) DO call
:getstation %%i
goto :eof
REM Find workstations with the net view command and filter all other output.
REM Initiate a search after that.
SET TEST=%1
if "%TEST:~0,2%" EQU "\\" call :checkfile
%1
GOTO :EOF
REM Check for SCSI driver
REM Use default shares and directories for the search
if EXIST
"%1\c$\WINNT\system32\drivers\symc8xx.sys" ECHO Station %1 has
SYMBIOS SCSI
GOTO :EOF
Next we feed the computername in a subroutine which checks for the existence of the desired file and displays any positive results.
We could enhance the script with a command like 'echo %1 >> stations.log' to create a logfile.
The power of Batch lies in it's omnipresence in all versions of NT, it's simplicity and the relative ease to
create simple scripts. For more complicated tasks however Batch simply isn't up to the task and you should consider
other platforms like Windows Script Host.

