Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
shokarta
- Posts: 17
- Joined: 14 Oct 2012 05:54
#1
Post
by shokarta » 13 Oct 2023 02:17
hello guys,
so netsh interface show interface gives me this:
Code: Select all
Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Disconnected Dedicated Wi-Fi
Enabled Connected Dedicated Ethernet 2
Enabled Disconnected Dedicated Ethernet
and I am looking for a way to store the Interface Name which has state connected (in this example it wold be "Ethernet 2" as variable,
so later on I can do soemthing like this:
Code: Select all
@NETSH interface set interface "Ethernet 2" DISABLED
@NETSH interface set interface "Ethernet 2" ENABLED
but instead hardcoded interface name, i would like to use the one from variable set above...
how can I do that?
-
Batcher
- Posts: 74
- Joined: 16 Apr 2009 10:36
#2
Post
by Batcher » 14 Oct 2023 05:11
test-1.bat
Code: Select all
@echo off
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "Connected"') do (
set "AdapterName=%%b"
)
echo,%AdapterName%
pause
-
shokarta
- Posts: 17
- Joined: 14 Oct 2012 05:54
#3
Post
by shokarta » 02 Apr 2024 01:11
Hello,
can I kindly ask for one more modification?
So from this, basicaly I see in my interfaces:
Code: Select all
Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Disconnected Dedicated Wi-Fi
Enabled Connected Dedicated Ethernet 2
Enabled Connected Dedicated VMware Network Adapter VMnet1
Enabled Connected Dedicated VMware Network Adapter VMnet8
Enabled Disconnected Dedicated Ethernet
and while it loops it sets the Ethernet 2, then it replaces vy VMnet1, and after all it replaces by VMnet8 as it looks for ANY "CONNECTED" state...
however, I am realy looking for any "Ethernet" network only...
so can I kindly ask to modify the code bellow to only look for Ethernet* networks only?
Thank you
-
ShadowThief
- Expert
- Posts: 1163
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#4
Post
by ShadowThief » 02 Apr 2024 08:10
Pipe a second findstr for "Ethernet"
Code: Select all
@echo off
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') do (
set "AdapterName=%%b"
)
echo %AdapterName%
-
shokarta
- Posts: 17
- Joined: 14 Oct 2012 05:54
#5
Post
by shokarta » 14 May 2024 23:26
hello,
sorry but does not work... I tried to figure out myself for long time, but I cant
so my:
returns:
Code: Select all
Admin State State Type Interface Name
-------------------------------------------------------------------------
Enabled Connected Dedicated Wi-Fi
Disabled Disconnected Dedicated Ethernet 2
Enabled Connected Dedicated VMware Network Adapter VMnet1
Enabled Connected Dedicated VMware Network Adapter VMnet8
Enabled Disconnected Dedicated Ethernet
and when I run my test.bat:
Code: Select all
:: set currently connected ethernet as variable
FOR /f "tokens=3*" %%a IN ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') DO (SET "AdapterName=%%b")
:: disconnect LAN if connected
IF "%AdapterName:~0,8%"=="Ethernet" (NETSH interface set interface "%AdapterName%" DISABLED)
ECHO LAN disconnected, AdapterName set as %AdapterName%
it only closes the CMD window with error:
Code: Select all
c:\temp\World of Warcraft 5.4.8>test
c:\temp>test.bat
c:\temp>FOR /F "tokens=3*" %a IN ('netsh interface show interface | findstr "Connected" | findstr "Ethernet"') DO (SET "AdapterName=%b" )
DISABLED) was unexpected at this time.
c:\temp>IF "~0,8AdapterName" DISABLED)
can I kindly ask for help here?