Page 1 of 1

SET variable in IF statement

Posted: 16 Nov 2016 12:31
by ImAhNoBoDy
I'm trying to figure out why the SET command in an IF statement will not change. The script just displays the IP address, subnet mask, and gateway. It also logs the results in a txt file.

Code: Select all

:@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET LOG="C:\LOGS\IPMATCH.TXT"

     SET "i=0"
    ::STORE FILENAMES INTO CONFIG ARRAY
    FOR /F "TOKENS=2 DELIMS=:" %%b in ('IPCONFIG^|FINDSTR /I "IPV4 SUBNET GATEWAY"') DO (
      SET CONFIG[!i!]=%%b & SET /A "i+=1"
    )

    ::DISPLAY ALL CONFIG ARRAY ITEMS
    SET CONFIG
    ECHO.===================================
    ::PRINT ARRAY ITEMS (FROM 0 TILL N)
    SET "len=!i!"
    SET "i=0"
    :LOOP1
    ECHO|SET /P="Found IP address:!CONFIG[%i%]!" >> %LOG%
    ECHO|SET /P="Found IP address:!CONFIG[%i%]!"
    ECHO.
    ::The problem starts here.
    IF !CONFIG[%i%]! EQU !CONFIG[%i%]! (ECHO IP ADDRESS IS: !CONFIG[%i%]!
    SET /A "i+=1"
    ECHO|SET /P=SUBNET MASK IS: !CONFIG[%i%]!" >> %LOG%
    ECHO|SET /P="SUBNET MASK IS: !CONFIG[%i%]!"^ & SET /A "i+=1"
    ECHO with GATEWAY OF : !CONFIG[%i%]!>> %LOG%
    ECHO with GATEWAY OF : !CONFIG[%i%]!
    ECHO.) ELSE (ECHO IP DOESN'T MATCH
    SET /A "i+=2"
    ECHO.)
    SET /A "i+=1"
    ECHO.
    IF %i% NEQ %len% GOTO LOOP1
   


The result I get is:

Code: Select all

IP ADDRESS IS:    192.168.1.100
SUBNET MASK IS:     192.168.1.100 with GATEWAY OF:    192.168.1.100


I don't get the actual subnet mask or gateway correct. I would like to get the correct information for both. Thanks.

Re: SET variable in IF statement

Posted: 16 Nov 2016 18:54
by penpen
This should be the same issue with the same solution as in your other thread, see:
http://www.dostips.com/forum/viewtopic.php?p=50140#p50140.


penpen

Re: SET variable in IF statement

Posted: 16 Nov 2016 19:39
by Compo
Why not just do this instead?

Code: Select all

@Echo Off
Set "LOG=C:\LOGS\IPMATCH.TXT"

(For /F Tokens^=2^,4^,8Delims^=^" %%A In ('
   "WMIC NICConfig Where IPEnabled=True Get DefaultIPGateway,IPAddress,IPSubnet"
') Do (Echo=IP ADDRESS IS:   %%B
   Echo SUBNET MASK IS:   %%C with GATEWAY OF:   %%A))>%LOG%
Type %LOG%
Timeout -1