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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.