我创建了这个 .bat 文件,通过单击正确的快捷方式即可调用,并且配置正确。但最后两次比较似乎没有得到正确的评估。
开始了。
@ECHO off
SETLOCAL EnableDelayedExpansion
SET landevcename='Imagine any name for a device'
SET landevcestatus=0
SET landevcestatusDisab=0
SET landevcestatusConn=2
SET landevcestatusT="NONE"
SET landevceidx=0
SET counter=1
ECHO Lan device to be checked is: %landevcename%
FOR /F "tokens=1" %%I IN ('wmic PATH win32_networkadapter where "Name=%landevcename%" get index') DO (
IF !counter!==2 SET /a landevceidx=%%I
SET /a counter+=1
)
ECHO Index of local area connection is: %landevceidx%
SET /a counter=1
FOR /F "tokens=1" %%J IN ('wmic PATH win32_networkadapter where "Name=%landevcename%" get netconnectionstatus') DO (
IF !counter!==2 SET /a landevcestatus=%%J
SET /a counter+=1
)
ECHO Status of local area connection is: %landevcestatus%
REM IF %landevcestatus%==%landevcestatusDisab% SET /a landevcestatusT=Disabled
IF %landevcestatus%==0 SET /a %landevcestatusT% "ItIsNotEnabled"
REM IF %landevcestatus%==%landevcestatusConn% SET /a landevcestatusT=Connected
**IF %landevcestatus%==2 SET /a %landevcestatusT% "ItIsEnabled"**
ECHO Text - Status of local area connection is: %landevcestatusT%
ECHO Status of local area connection is: %landevcestatus%
CMD
@ECHO off
这两者:
如果 %landevcestatus%==0,则设置 /a %landevcestatusT%“ItIsNotEnabled”
如果 %landevcestatus%==2,则设置 /a %landevcestatusT%“ItIsEnabled”
打字错误可能出在哪里?
答案1
打字错误可能出在哪里?
更正
SET landevcename='Imagine any name for a device'
应该:
SET landevcename="Imagine any name for a device"
如果您使用,'
那么您需要在命令'
中退出for
。
IF !counter!==2 SET /a landevceidx=%%I
应该:
IF !counter!==2 SET landevceidx=%%I
/a
用于数值表达式不是字符串赋值。
IF !counter!==2 SET /a landevcestatus=%%J
应该:
IF !counter!==2 SET landevcestatus=%%J
SET /a counter+=1
应该:
SET /a "counter+=1"
IF %landevcestatus%==0 SET /a %landevcestatusT% "ItIsNotEnabled"
应该:
IF %landevcestatus%==0 SET landevcestatusT="ItIsNotEnabled"
IF %landevcestatus%==2 SET /a %landevcestatusT% "ItIsEnabled"
应该:
IF %landevcestatus%==2 SET landevcestatusT="ItIsEnabled"
FOR /F "tokens=1" %%I IN ('wmic PATH win32_networkadapter where "Name=%landevcename%" get index') DO (
应该:
FOR /F "tokens=1" %%I IN ('wmic PATH win32_networkadapter where Name^=%landevcename% get index') DO (
您需要转义=
using ^
,并删除 s "
(它们是变量的一部分%landevcename%
)。
简化的批处理文件
您可以使用单个for /f
命令和更少的变量来执行您想要的操作。您不需要索引或计数器。
使用以下批处理文件并进行landevicename
适当设置。
局域网状态命令:
@echo off
setlocal
set landevcename="Remote NDIS based Internet Sharing Device"
echo Lan device to be checked is: %landevcename%
rem skip first line
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1,2" %%i IN (`wmic PATH win32_networkadapter where Name^=%landevcename% get netconnectionstatus^, index ^| findstr /r /v "^$"`) DO ( set landevceidx=%%i
set landevcestatus=%%j
)
if %landevcestatus%==2 (
set landevcestatusT="ItIsEnabled"
) else (
set landevcestatusT="ItIsNotEnabled"
)
echo Text - Status of local area connection is: %landevcestatusT%
echo Status of local area connection is: %landevcestatus%
echo Index of local area connection is: %landevceidx%
endlocal
示例输出:
F:\test>lanstatus
Lan device to be checked is: "Remote NDIS based Internet Sharing Device"
Text - Status of local area connection is: "ItIsEnabled"
Status of local area connection is: 2
Index of local area connection is: 17