i have been trying to do a batch that does an ipconfig and gets the ip. it then matches the ip to a value set. displaying if the ip matches or not. the closest thing i found was in another post which si
@echo off
rem --- complete adapter name to find without the ending ":" ---
set adapter=Wireless LAN adapter Wireless Network Connection
rem --- token under an adapter to extract IP address from ---
set IPAddrToken=IPv4 Address
rem --- token under an adapter to extract IP address from ---
set matchipaddress=192.168.1.101
setlocal enableextensions enabledelayedexpansion
set adapterfound=false
set emptylines=0
set ipaddress=
for /f "usebackq tokens=1-3 delims=:" %%e in (`ipconfig ^| findstr /n "^"`) do (
set "item=%%f"
if /i "!item!"=="!adapter!" (
set adapterfound=true
set emptylines=0
) else if not "!item!"=="" if not "!item!"=="!item:%IPAddrToken%=!" if "!adapterfound!"=="true" (
@rem "!item:%IPAddrToken%=!" --> item with "IPv4 Address" removed
set ipaddress=%%g
goto :result
)
if "%%f-%%g-!adapterfound!-!emptylines!"=="--true-1" (
@rem 2nd blank line after adapter found
goto :result
)
if "%%f-%%g-!adapterfound!-!emptylines!"=="--true-0" (
@rem 1st blank line after adapter found
set emptylines=1
)
)
endlocal
:result
echo %adapter%
echo.
if not "%ipaddress%"=="" (
echo %IPAddrToken% =%ipaddress%
) else (
if "%adapterfound%"=="true" (
echo %IPAddrToken% Not Found
) else (
echo Adapter Not Found
)
)
ECHO.
PAUSE
sure this might do a bit more but looking into a specific adapter and seeing if i have an ip or not and if i do have an ip make sure its the set ip.
thank you in advance!
This can be done with a one-liner.
The
&&
operator evaluates upon a successful return of thefind
command. However, iffind
fails (which is true if no matches), the stuff after||
gets evaluated instead. This is basically a shorthand form of the following:Using
find
's return code (its%ERRORLEVEL%
) is very handy for determining whether a string exists within another string.For more info on conditional execution, read this.
Edit: O.P. commented, "I have 1 usb wifi adapter, internal wifi adapter, and ethernet port, i would love to have each one check for the specific ip...." Here's a basic skeleton you can use to build your project. Use something like
echo !Description! | find "wlan card identifier"
with conditional execution demonstrated above to take whatever action you wish. Happy coding! :)