感谢8楼提供的测试用例!复制代码
- @echo off
- rem 获取本地连接名称
- rem WinXP SP2 EN 测试通过
- rem WinXP SP3 CS 测试通过
- reg export "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards" "%temp%\MyNet.reg" >nul
- for /f "tokens=1 delims=:" %%a in ('type "%temp%\MyNet.reg"^|findstr /n Ethernet') do (
- set "DstRow=%%a"
- goto :next
- )
- goto :eof
- :next
- set /a SkpRow=DstRow-2
- for /f "skip=%SkpRow% tokens=2 delims={}" %%a in ('type "%temp%\MyNet.reg"^|findstr .*') do (
- for /f "skip=6 tokens=*" %%b in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{%%a}\Connection"') do (
- set LcoNet=%%b
- call echo %%LcoNet:~12%%
- goto :eof
- )
- )
中文版系统测试通过,英文版我没有测试环境,还请BATCHER兄测试。复制代码
- @echo off
- for /f "tokens=2 delims==" %%a in ('wmic nic get netconnectionid /value') do set "var=%%a"&call echo %%var%%&pause>nul&exit/b
复制代码
- C:\Documents and Settings\Administrator\桌面>(for /F "skip=6 tokens=*" %b in ('r
- eg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-
- E325-11CE-BFC1-08002BE10318}\{5FBB2E38-FBE8-4F2D-908A-88FA06960529}\Connection"'
- ) do (
- set LcoNet=%b
- call set LcoNet=%LcoNet: =%
- call echo %LcoNet:~10%
- goto :eof
- ) )
- 错误: 系统找不到指定的注册表项或值
复制代码
- Microsoft Windows XP [版本 5.1.2600]
- (C) 版权所有 1985-2001 Microsoft Corp.
- C:\Documents and Settings\Administrator\桌面>reg query "HKEY_LOCAL_MACHINE\SYSTE
- M\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{5FBB
- 2E38-FBE8-4F2D-908A-88FA06960529}\Connection"
- 错误: 系统找不到指定的注册表项或值
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Test>test.bat
ECHO is off.
C:\Test>wmic nic get netconnectionid /value NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID=Local Area Connection NetConnectionID= NetConnectionID=Wireless Network Connection NetConnectionID= NetConnectionID=1394 Connection NetConnectionID= NetConnectionID= NetConnectionID= NetConnectionID=VMware Network Adapter VMnet1 NetConnectionID=VMware Network Adapter VMnet8 NetConnectionID= |
复制代码
- Windows Registry Editor Version 5.00
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}]
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\Descriptions]
- "Broadcom NetLink (TM) Gigabit Ethernet"=hex(7):31,00,00,00,00,00
- "Intel(R) PRO/Wireless 3945ABG Network Connection"=hex(7):31,00,00,00,00,00
- "1394 网络适配器"=hex(7):31,00,00,00,00,00
- "数据包计划程序微型端口"=hex(7):31,00,00,00,32,00,00,00,33,00,00,00,34,00,00,00,35,00,00,\
- 00,00,00
- "WAN 微型端口 (Network Monitor)"=hex(7):31,00,00,00,00,00
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{5765CB79-7D9F-443E-849D-51EAB9A8895E}]
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{5765CB79-7D9F-443E-849D-51EAB9A8895E}\Connection]
- "PnpInstanceID"="PCI\\VEN_8086&DEV_4222&SUBSYS_10028086&REV_02\\4&192AC53F&0&00E0"
- "MediaSubType"=dword:00000002
- "Name"="无线网络连接"
- "IpCheckingEnabled"=dword:00000000
- "ShowIcon"=dword:00000001
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{5D4BF871-B3AC-4806-869C-B859C0B73BC3}]
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{5D4BF871-B3AC-4806-869C-B859C0B73BC3}\Connection]
- "PnpInstanceID"="PCI\\VEN_14E4&DEV_169C&SUBSYS_207517AA&REV_03\\4&6B16D5B&0&38F0"
- "MediaSubType"=dword:00000001
- "Name"="本地连接"
- "IpCheckingEnabled"=dword:00000000
- "ShowIcon"=dword:00000001
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{7F63BF53-CFEF-482C-95E8-DBE1B67EFB39}]
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{7F63BF53-CFEF-482C-95E8-DBE1B67EFB39}\Connection]
- "PnpInstanceID"="V1394\\NIC1394\\C1C18EC09F00"
- "MediaSubType"=dword:00000005
- "Name"="1394 连接"
- "IpCheckingEnabled"=dword:00000000
复制代码
- Windows Registry Editor Version 5.00
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\10]
- "ServiceName"="{5765CB79-7D9F-443E-849D-51EAB9A8895E}"
- "Description"="Intel(R) PRO/Wireless 3945ABG Network Connection"
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\11]
- "ServiceName"="{7F63BF53-CFEF-482C-95E8-DBE1B67EFB39}"
- "Description"="1394 网络适配器"
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\2]
- "ServiceName"="{5D4BF871-B3AC-4806-869C-B859C0B73BC3}"
- "Description"="Broadcom NetLink (TM) Gigabit Ethernet"
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\9]
- "ServiceName"="{5FBB2E38-FBE8-4F2D-908A-88FA06960529}"
- "Description"="Realtek RTL8139 Family PCI Fast Ethernet NIC"
复制代码
- @echo off
- setlocal enabledelayedexpansion
- set count=0
- for /f "delims=:" %%a in ('ipconfig^|find /i "Ethernet adapter"') do (
- set /a count+=1
- call :GetName "%%a" !count!
- )
- for /f "tokens=2 delims==" %%a in ('set LocalConn_') do (
- echo/%%a
- )
- goto :eof
- :GetName
- set "str=%1"
- set LocalConn_%2=%str:~18,-1%
C:\Test>netsh interface show interface Admin State State Type Interface Name ------------------------------------------------------------------------- Enabled Dedicated Wireless Network Connection Enabled Dedicated Local Area Connection Enabled Dedicated VMware Network Adapter VMnet1 Enabled Dedicated VMware Network Adapter VMnet8 Enabled Dedicated 1394 Connection Enabled Internal Internal Enabled Loopback Loopback |
复制代码
- @echo off
- for /f "delims=:" %%I in ('ipconfig ^| find /i "Ethernet"') do set LAC=%%I
- set LAC=%LAC:~17%
- echo %LAC%
- pause
Ps:繁体系统中显示为介面,简体则为接口,不过win7好像会有一个Loopback Pseudo-Interface的回环网卡,所以代码实际使用可能需要加入排除判断,比如下面使用中,我仅设置第一块网卡,然后跳出for循环。复制代码
- rem 以上内容略
- for /f tokens^=1^,2^ delims^=^" %%j in ('netsh interface ip show config') do (
- if "%%j"=="介面 " (
- echo 连线名称为:%%k
- )
- if "%%j"=="接口 " (
- echo 连线名称为:%%k
- )
- )
- pause
有兴趣的可以到原文看看:http://zhangge.net/1885.html复制代码
- .....以上略....
- rem 獲取第一塊網卡的名稱,并設置IP,多塊網卡將仅設置第一塊
- for /f tokens^=1^,2^ delims^=^" %%j in ('netsh interface ip show config') do (
- if "%%j"=="介面 " (
- netsh interface ip set address name="%%k" source=static addr=!addr! mask=!mask! gateway=!gateway!
- call :end
- )
- if "%%j"=="接口 " (
- netsh interface ip set address name="%%k" source=static addr=!addr! mask=!mask! gateway=!gateway!
- call :end
- )
- )
- :end
- echo 設置完畢,3S后自動退出!
- ping -n 3 127.1 >nul
- exit
- ...以下略....
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |