SET variable in IF statement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ImAhNoBoDy
Posts: 11
Joined: 16 Nov 2016 12:03

SET variable in IF statement

#1 Post by ImAhNoBoDy » 16 Nov 2016 12:31

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.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: SET variable in IF statement

#2 Post by penpen » 16 Nov 2016 18:54

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

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: SET variable in IF statement

#3 Post by Compo » 16 Nov 2016 19:39

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

Post Reply