Batch to the Roots! Page 2

By ServerWatch Staff (Send Email)
Posted Jan 31, 2001


Wim Verveen

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
FOR %%i IN ("c:\winnt\*.*") DO call :sub %%i
goto :eof

:sub
if /I "%~x1" NEQ ".EXE" echo %~f1
goto :eof

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.
A simple way to do this would be to check for the existence of the file SYMC8xx.SYS

@echo off
FOR /F "usebackq tokens=1" %%i IN (`net view`) DO call :getstation %%i
goto :eof

:getstation
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

:checkfile
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

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).
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.

As you can see Batch isn't dead yet and still can have uses where it outperforms more complicated languages.
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.

Page 2 of 2


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.