Batch file to compare pc ip address to string

Posted on 2014-08-08
Last Modified: 2014-08-08
I am attempting to find out if the ip address of the computer matches a certain subnet so I can perform specific actions.

I am using the information from here:

My problem is getting the ip address and matching it against a string of the first three octets. 192.168.1 vs 192.168.2

Can you provide example for this? if first three octets = 192168.1 do something else do something different sort of logic?
Question by:zequestioner
    LVL 82

    Assisted Solution

    Try the script below; it uses WMI to query for the IP addresses, so neither different OS versions nor localized OS languages should make a difference.
    The script doesn't just tests the octets, it calculates the real subnet, so even subnets like should be processed correctly. Furthermore, it checks all IP enabled NICs, so machines with more than 1 adapter should be processed correctly as well.
    @echo off
    setlocal enabledelayedexpansion
    REM *** Add common code here
    call :GetIPConfig
    call :CheckSubnet && goto Range1
    call :CheckSubnet && goto Range1
    echo No supported IP range found.
    goto :eof
    echo You're in range 1
    REM *** Add your code for range 1 here ...
    goto :eof
    echo You're in range 2
    REM *** Add your code for range 2 here ...
    goto :eof
    REM ====================================================================================================
    REM Only functions below this line!
    REM ====================================================================================================
    goto :eof
    set /a NicCount = 0
    for /f "skip=1 tokens=2,4 delims={}" %%a in ('wmic.exe nicconfig where "IPEnabled=true" get Caption^,IPAddress^,IPSubnet') do (
    	set /a NicCount += 1
    	for /f "tokens=1 delims=, " %%i in ("%%a") do (
    		set IPv4[!NicCount!]=%%~i
    		for /f "tokens=1-4 delims=." %%s in ("%%~i") do (
    			set sn1=%%s
    			set sn2=%%t
    			set sn3=%%u
    			set sn4=%%v
    	for /f "tokens=1 delims=, " %%i in ("%%b") do (
    		set NMv4[!NicCount!]=%%~i
    		for /f "tokens=1-4 delims=." %%s in ("%%~i") do (
    		  set /a sn1 "&=" %%s
    		  set /a sn2 "&=" %%t
    		  set /a sn3 "&=" %%u
    		  set /a sn4 "&=" %%v
    	set SNv4[!NicCount!]=!sn1!.!sn2!.!sn3!.!sn4!
    for /l %%i in (1, 1, %NicCount%) do (
    	echo Found IP Address !IPv4[%%i]!, Netmask !NMv4[%%i]!, Subnet !SNv4[%%i]!
    goto :eof
    set Subnet=%~1
    set Return=1
    for /l %%i in (1, 1, %NicCount%) do (
    	if %Subnet%==!SNv4[%%i]! (set Return=0)
    exit /b %Return%

    Open in new window

    LVL 1

    Author Comment

    Thank you much. This works but it seems a bit extreme. I already know the ip address range and there is only /24. there is only 1 nic too. I would like somethink much eeasier to read but this will work for me. thank you again very much.

    i do not know the right code syntax but here is what i was trying to do:

    set ipsubnet=192.168.1

    if %ipsubnet% == 192.168.1 (
         Echo . install.exe
    ) else (
         goto end


    but I do not know how to match if the ip address matches the first three octets. since ip subnet with the ipsubnet vairable.

    thank you agian.
    LVL 43

    Accepted Solution


    On mobile at mo. But see my script here.

    LVL 1

    Author Comment

    Thank both you. The first does exactly what i need and more complete but i can get your script better. but i still need more work to make it work for me.
    LVL 1

    Author Comment

    LVL 43

    Expert Comment

    by:Steve Knight
    BTW you can get the first part of a variable with %variable:~0,X% aswell as splitting up using for loops at the "." etc.

    i.e. if you have IPADDRESS=

    then %IPADDRESS:~0,10% is 192.168.1.  - better make 10 digits so you can tell on 192.168.1. rather than etc.

    So you could use:

    if "%ipaddress:~0,10%"=="192.168.1." (
      echo on subnet 192.168.1.x


    SET /? from cmd prompt shows you more such options.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    array6 challenfge 6 48
    Windows Batch File - Combing output in a single line 3 41
    mapShare challenge 13 41
    regex to extract ip:john 17 38
    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    A short article about a problem I had getting the GPS LocationListener working.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now