Change IP with bat file using variables

I am using the follow script to change my IP info.

I would like to get the name of the existing network adapter, set a variable with that name and replace name="Local Area Connection" with something like name=%nicName%

How can I do this?
REM ***** Change IP back to DHCP ******

@ECHO OFF
         
ECHO Setting IP Address and Subnet Mask
netsh interface ip set address name="Local Area Connection" source=dhcp
     
ECHO Setting Primary DNS
netsh int ip set dns name="Local Area Connection" static 10.0.1.1 
     
ECHO Setting Secondary DNS
netsh int ip add dns name=Local Area Connection" 10.0.1.2 index=2

ECHO Setting WINS to DHCP
netsh int ip set wins name="Local Area Connection" source=dhcp

ECHO Here are the new settings for %computername%:
netsh int ip show config
     
pause

Open in new window

Phase2Asked:
Who is Participating?
 
Giovanni HewardCommented:
Err... here is the corrected version
@echo off
setlocal enabledelayedexpansion
for /f "skip=2 tokens=4-6" %%a in ('netsh interface show interface') do (
	set nicName=%%a %%b %%c
	echo "!nicName!"
)
pause

Open in new window

0
 
Giovanni HewardCommented:
Here's one way to do it...
@echo off
for /f "skip=2 tokens=4,5,6" %%a in ('netsh interface show interface') do (
     set nicName=%a %b %c

     echo Setting IP Address and Subnet Mask
     netsh interface ip set address name="%nicName%" source=dhcp
     
     echo Setting Primary DNS
     netsh int ip set dns name="%nicName%" static 10.0.1.1 
     
     echo Setting Secondary DNS
     netsh int ip add dns name="%nicName%" 10.0.1.2 index=2

     echo Setting WINS to DHCP
     netsh int ip set wins name="%nicName%" source=dhcp

     echo Here are the new settings for %computername%:
     netsh int ip show config
     
)
pause

Open in new window

0
 
Giovanni HewardCommented:
Here is an alternative method of extracting the NIC name(s)...
@echo off
setlocal enabledelayedexpansion
for /f "skip=3 tokens=*" %%a in ('netsh interface show interface') do (
	set nicName=%%a
	set nicName=!NicName:Enabled=!
	set nicName=!NicName:Disconnected=!
	set nicName=!NicName:Connected=!
	set nicName=!NicName:Dedicated=!
	for /f "tokens=* delims= " %%n in ("!nicName!") do set nicName=%%n
	echo [!nicName!]
)
pause

Open in new window

0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Phase2Author Commented:
Hi X
Thanks for the script. Below is what I am using. This extracts the NIC name just fine but doesnt seem to do the rest with setting IPs.

Also I only need it to find the name of the first NIC it finds.

@echo off
setlocal enabledelayedexpansion
for /f "skip=2 tokens=4-6" %%a in ('netsh interface show interface') do (
	set nicName=%%a %%b %%c
	echo "!nicName!"
)

     echo Setting IP Address and Subnet Mask
     netsh interface ip set address name="%nicName%" source=dhcp
     
     echo Setting Primary DNS
     netsh int ip set dns name="%nicName%" static 59.106.192.1 
     
     echo Setting Secondary DNS
     netsh int ip add dns name="%nicName%" 59.106.192.2 index=2

     echo Setting WINS to DHCP
     netsh int ip set wins name="%nicName%" source=dhcp

     echo Here are the new settings for %computername%:
     netsh int ip show config
     
)
pause

Open in new window

0
 
Phase2Author Commented:
OK figured out the issue.

%%c  was adding an extra space in the nicName variable. My nicName is actually in Japanese so the format is a little different.

¿¿¿¿ ¿¿¿¿¿

3words but only one space in the name.

Thank you very much!

Below is the code that works for me



@echo off
setlocal enabledelayedexpansion
for /f "skip=2 tokens=4-6" %%a in ('netsh interface show interface') do (
	set nicName=%%a %%b
)

     echo Setting IP Address and Subnet Mask
     netsh interface ip set address name="%nicName%" source=dhcp
     
     echo Setting Primary DNS
     netsh int ip set dns name="%nicName%" static 59.106.192.1 
     
     echo Setting Secondary DNS
     netsh int ip add dns name="%nicName%" 59.106.192.2 index=2

     echo Setting WINS to DHCP
     netsh int ip set wins name="%nicName%" source=dhcp

     echo Here are the new settings for %computername%:
     netsh int ip show config
     

DEL "%~f0"

Open in new window

0
 
Phase2Author Commented:
For those trying to use this script make sure you take off the

DEL "%~f0"

at the end or else your bat file will delete after you run it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.