中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-05 10:26
48,038 主题排行 / 350,123 发帖 / 今日 1 篇 / 48,251 会员排行
DOS批处理 & 脚本技术(批处理室) » (求教)检测IP格式是否正确批处理的问题
可打印版本  717 / 6
第1楼 lg560852 发表于 2007-07-27 11:23
初级用户 发帖 26 积分 60
(求教)检测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 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楼 lg560852 发表于 2007-07-27 11:26
初级用户 发帖 26 积分 60
这是偶又调试时的情况
修改后:
::@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楼 lg560852 发表于 2007-07-27 11:28
初级用户 发帖 26 积分 60
这里为什么又调用了一次check
第4楼 wudixin96 发表于 2007-07-27 11:35
银牌会员 发帖 931 积分 1,928
因为这里call :check %_input%了

call完后,就自动回到call的下一行了
第5楼 lg560852 发表于 2007-07-27 11:40
初级用户 发帖 26 积分 60
那应该怎么改呢??
第6楼 wudixin96 发表于 2007-07-27 11:42
银牌会员 发帖 931 积分 1,928
call :check %_input%
goto end
第7楼 lg560852 发表于 2007-07-27 11:45
初级用户 发帖 26 积分 60
多谢达人指教!!
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023