You are not logged in.

#1 19 Mar 2015 19:17

paul0041
Member
Registered: 19 Mar 2015
Posts: 21

set variable w/ netsh & findstr

This works if you don't care which adapter you are setting %ip_address% to equal its IPv4 address.  It will set the last IPv4 Address it finds in the output of ipconfig

set ip_address_string="IPv4 Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do set ip_address=%%f


and this works when run standalone:
set ip_address_string="IP Address"
netsh interface ip show addresses name="Wireless Network Connection" | findstr /c:%ip_address_string%

output is: IP Address:               10.x.x.x

so when using netsh instead of config, it fails to find any tokens:

set ip_address_string="IP Address"
for /f "usebackq tokens=2 delims=:" %%f in (`netsh interface ip show addresses name="Wireless Network Connection" ^| findstr /c:%ip_address_string%`) do set ip_address=%%f

What am I doing wrong?

Offline

#2 03 May 2016 14:12

elias
Member
Registered: 03 May 2016
Posts: 12

Re: set variable w/ netsh & findstr

Following the debugging and troubleshooting tips from the Batchography book to figure out the problem, the issue is with the '=' sign getting omitted smile

@echo off
set ip_address_string="IP Address"
for /f "usebackq tokens=* delims=" %%f in (
	`netsh interface ip show addresses name="Ethernet0"`) do (
	set ip_address=%%f
	echo %%f
)

Displayed:

he syntax supplied for this command is not valid. Check help for the correct syntax.
Usage: show addresses  [[name=]<string>]
Parameters:
       Tag         Value
       name      - The name or index of a specific interface.
Remarks: Displays the IP address configuration for an interface or
         interfaces.
The information displayed for this command consists of:
Field              Description
-----              -----------
DHCP enabled       Shows whether the address comes from static or DHCP
                   configuration.
IP Address         Shows the IP address configured for an interface.
Subnet Mask        Shows the subnet mask associated with the IP address.
Default Gateway    Shows the IP address of a default gateway for the interface.
Gateway Metric     Shows the metric for the default gateway shown above.
                   Only applies if multiple default gateways are configured.
Interface Metric   Shows the metric for an interface.
                   Only applies if multiple interfaces are configured.
Examples:
       show addresses "Wired Ethernet Connection"

Which showed that the syntax to 'netsh' was incorrect.

The solution: just escape the '=' sign:

netsh interface ip show addresses name^="Wireless Network Connection"

Book: Batchography - The Art of Batch Files Programming

Offline

Board footer

Powered by