返回列表 发帖

[文件操作] [已解决]批处理if判断条件失效,半瓶醋学员,请大家指点指教

代码目的:判断符合条件的ip地址就执行相应的程序,如果不符合条件就执行正常程序,以下是问题代码:
cd /d %~dp0
for /f "delims=: tokens=1,2" %%a in ('ipconfig ^|find "IPv4"') do echo,%%b&set ips=%%b
echo %ips%
if %ips% == 192.168.1.100 goto AA
if %ips% == 192.168.1.101 goto BB
echo normal machine
goto normal
:AA
start test1.exe
:BB
start test2.exe
:normal
start normal.exe
pauseCOPY
以上代码执行情况是:
如果ip地址是100和101以外的地址,那么会执行normal.exe,这里没问题
如果ip地址是100,那么会执行test1.exe 然后再执行normal.exe
如果ip地址是101,那么会执行test2.exe 然后再执行normal.exe
以上代码用过if。。。else的写法,结果同样是这样
实在无解,也看不出问题,还请大家赐教,感谢!
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

本帖最后由 ygqiang 于 2024-10-24 14:34 编辑
@echo off & setlocal enabledelayedexpansion
echo 代码开始
cls
for /f "delims=: tokens=1,2" %%a in ('ipconfig ^|find "IPv4"') do set ips=%%b
echo %ips%
if %ips% == 10.16.13.94 goto bb
if %ips% == 192.168.1.100 goto AA
if %ips% == 192.168.1.101 goto BB
echo normal machine
goto normal
:AA
echo start test1.exe
pause
exit
:BB
echo start test2.exe
pause
exit
:normal
echo start normal.exe
pause
exitCOPY
1

评分人数

TOP

回复 2# ygqiang


    感谢参与讨论,上面代码修改后,只是输出了echo后面的字符,exe实际并没有执行

TOP

回复 2# ygqiang




    @echo off & setlocal enabledelayedexpansion
echo 代码开始
cls

for /f "delims=: tokens=1,2" %%a in ('ipconfig ^|find "IPv4"') do set ips=%%b
echo %ips%
if %ips% == 10.16.13.94 goto bb
if %ips% == 192.168.1.100 goto AA
if %ips% == 192.168.1.101 goto BB
echo normal machine
goto normal

:AA
start test1.exe
exit

:BB
start test2.exe
exit

:normal
start normal.exe
exit

感谢启发,代码修改如上,通过测试,学习还是要仔细一些

TOP

返回列表