user2881189
user2881189

Reputation: 11

Eliminating redundant batch script

I am new to batch and need some guidance on how to make my script shorter... it works ok but theres alot of repeated lines .

The script sends 'arp -a' to temp file for parse then goes thorugh the 6 IP address to check if they are static or not .

Any advise to create shorter better method would greatly be appreciated . Thank-you !


@ECHO OFF

cls

ECHO ---------------Check SiteC IP = Static -----------------

ARP -a > O:\temp_log.txt

SET /A var1 = 0
SET /A var2 = 0
SET /A var3 = 0
SET /A var4 = 0
SET /A var5 = 0
SET /A var6 = 0


FOR /F "tokens=1,3 delims= " %%A IN ('findstr "static" "O:\temp_log.txt"') DO IF "%% A"=="192.168.0.11" ECHO %%A = %%B && SET /A var1 = 1 
IF %var1% == 1 ( ECHO Match ) ELSE ( ECHO SiteC1 FAIL )

ECHO --

FOR /F "tokens=1,3 delims= " %%A IN ('findstr "static" "O:\temp_log.txt"') DO IF "%%A"=="192.168.0.12" ECHO %%A = %%B && SET /A var2 = 1 
IF %var2% == 1 ( ECHO Match ) ELSE ( ECHO SiteC2 FAIL )

ECHO --

FOR /F "tokens=1,3 delims= " %%A IN ('findstr "static" "O:\temp_log.txt"') DO IF "%%A"=="192.168.0.13" ECHO %%A = %%B && SET /A var3 = 1 
IF %var3% == 1 ( ECHO Match ) ELSE ( ECHO SiteC2 FAIL )

ECHO --


FOR /F "tokens=1,3 delims= " %%A IN ('findstr "static" "O:\temp_log.txt"') DO IF "%%A"=="192.168.0.14" ECHO %%A = %%B && SET /A var4 = 1 
IF %var4% == 1 ( ECHO Match ) ELSE ( ECHO SiteC2 FAIL )

ECHO --

FOR /F "tokens=1,3 delims= " %%A IN ('findstr "static" "O:\temp_log.txt"') DO IF "%%A"=="192.168.0.15" ECHO %%A = %%B && SET /A var5 = 1 
IF %var5% == 1 ( ECHO Match ) ELSE ( ECHO SiteC5 FAIL )

ECHO --


FOR /F "tokens=1,3 delims= " %%A IN ('findstr "static" "O:\temp_log.txt"') DO IF "%%A"=="192.168.0.16" ECHO %%A = %%B && SET /A var6 = 1 
IF %var6% == 1 ( ECHO Match ) ELSE ( ECHO SiteC6 FAIL )


ECHO.
ECHO.

pause

Upvotes: 1

Views: 74

Answers (1)

Magoo
Magoo

Reputation: 80033

@ECHO OFF
SETLOCAL
FOR /l %%s IN (1,1,6) DO SET "var%%s=Site C%%s FAIL"
FOR /f %%A IN ('arp -a ^|findstr "static"') DO (
 FOR /l %%s IN (1,1,6) DO ( ECHO %%A
  IF %%A==192.168.0.1%%s SET var%%s=Site C%%s Match
 )
)
FOR /l %%s IN (1,1,6) DO CALL ECHO %%var%%s%%
GOTO :EOF

This should substitute.

The first FOR sets VARn to "Site Cn FAIL" for n=1 to 6. The next FOR interprets the output of ARP, filtered for "static" The caret (^) before the pipe tells batch that the pipe is part of the command, not the FOR. The neste FOR loops through the strings 192.168.0.1`` to192.168.0.16and if that matches the first token from theARPoutput (in%%A) then the appropriate variable is set to...Match`

Finally, the contents of var1..var6 are echoed.

There appears to be no reason for token 3, since %%B would only ever be "static", given your filter. Equally, space is a defult delimiter and none of the other defaults would occur in an ARP output line as fitered.

Upvotes: 1

Related Questions