中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-08-05 11:24
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » (求教)检测IP格式是否正确批处理的问题 查看 719 回复 6
楼 主 (求教)检测IP格式是否正确批处理的问题 发表于 2007-07-27 11:23 ·  中国 山东 济南 教育网
初级用户
积分 60
发帖 26
注册 2006-12-31 06:34
19年会员
UID 75077
性别 男
状态 离线
@echo off
:: 检测IP格式是否标准
:: code by jm 2006-10-23
:begin
cls
set input=
set /p input=请输入IP:
echo %input%|findstr "^*\.*\.*\.*$">nul||goto fail
set _input=%input:.= %
call :check %_input%

:check
if "%4"=="" goto fail
for %%i in (%1 %2 %3 %4) do (
if %%i gtr 255 goto fail
)
cls
echo %input% 是正确的IP
echo.
pause
goto begin

:fail
cls
echo %input% 是错误的IP
echo.
pause
goto begin



这段代码如何改才能实现只判断一次就退出??
偶改完后发现如果IP正确要再判断一次!
改后:
@echo off
:: 检测IP格式是否标准
:: code by jm 2006-10-23
:begin
cls
set input=
set /p input=请输入IP:
echo %input%|findstr "^*\.*\.*\.*$">nul||goto fail
set _input=%input:.= %
call :check %_input%

:check
if "%4"=="" goto fail
for %%i in (%1 %2 %3 %4) do (
if %%i gtr 255 goto fail
)
cls
echo %input% 是正确的IP
echo.
pause
goto end

:fail
cls
echo %input% 是错误的IP
echo.
pause


:end

[ Last edited by lg560852 on 2007-7-27 at 11:26 AM ]
2 发表于 2007-07-27 11:26 ·  中国 山东 济南 教育网
初级用户
积分 60
发帖 26
注册 2006-12-31 06:34
19年会员
UID 75077
性别 男
状态 离线
这是偶又调试时的情况
修改后:
::@echo off
:: 检测IP格式是否标准
:: code by jm 2006-10-23
:begin
::cls
set input=
set /p input=请输入IP:
echo %input%|findstr "^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$">nul||goto fail
set _input=%input:.= %
call :check %_input%

:check
if "%4"=="" goto fail
for %%i in (%1 %2 %3 %4) do (
if %%i gtr 255 goto fail
)
::cls
echo %input% 是正确的IP
echo.
pause
goto end

:fail
::cls
echo %input% 是错误的IP
echo.
pause


:end


执行结果:

C:\Documents and Settings\桌面>set input=

C:\Documents and Settings\桌面>set /p input=请输入IP:
请输入IP:1.1.1.1

C:\Documents and Settings\桌面>echo 1.1.1.1 | findstr "^[0-9]*\.[0-9]*
9]*\.[0-9]*$" 1>nul || goto fail

C:\Documents and Settings\桌面>set _input=1 1 1 1

C:\Documents and Settings\桌面>call :check 1 1 1 1

C:\Documents and Settings\桌面>if "1" == "" goto fail

C:\Documents and Settings\桌面>for %i in (1 1 1 1) do (if %i GTR 255 go
il )

C:\Documents and Settings\桌面>(if 1 GTR 255 goto fail )

C:\Documents and Settings\桌面>(if 1 GTR 255 goto fail )

C:\Documents and Settings\桌面>(if 1 GTR 255 goto fail )

C:\Documents and Settings\桌面>(if 1 GTR 255 goto fail )

C:\Documents and Settings\桌面>echo 1.1.1.1 是正确的IP
1.1.1.1 是正确的IP

C:\Documents and Settings\桌面>echo.


C:\Documents and Settings\桌面>pause
请按任意键继续. . .

C:\Documents and Settings\桌面>goto end

C:\Documents and Settings\桌面>if "" == "" goto fail

C:\Documents and Settings\桌面>echo 1.1.1.1 是错误的IP
1.1.1.1 是错误的IP

C:\Documents and Settings\桌面>echo.


C:\Documents and Settings\桌面>pause
请按任意键继续. . .
3 发表于 2007-07-27 11:28 ·  中国 山东 济南 教育网
初级用户
积分 60
发帖 26
注册 2006-12-31 06:34
19年会员
UID 75077
性别 男
状态 离线
这里为什么又调用了一次check
4 发表于 2007-07-27 11:35 ·  中国 浙江 杭州 电信
银牌会员
★★★
积分 1,928
发帖 931
注册 2007-01-06 11:46
19年会员
UID 75624
性别 男
状态 离线
因为这里call :check %_input%了

call完后,就自动回到call的下一行了
5 发表于 2007-07-27 11:40 ·  中国 山东 济南 教育网
初级用户
积分 60
发帖 26
注册 2006-12-31 06:34
19年会员
UID 75077
性别 男
状态 离线
那应该怎么改呢??
6 发表于 2007-07-27 11:42 ·  中国 浙江 杭州 电信
银牌会员
★★★
积分 1,928
发帖 931
注册 2007-01-06 11:46
19年会员
UID 75624
性别 男
状态 离线
call :check %_input%
goto end
7 发表于 2007-07-27 11:45 ·  中国 山东 济南 教育网
初级用户
积分 60
发帖 26
注册 2006-12-31 06:34
19年会员
UID 75077
性别 男
状态 离线
多谢达人指教!!
论坛跳转: