Board logo

标题: (求教)检测IP格式是否正确批处理的问题 [打印本页]

作者: lg560852     时间: 2007-7-27 11:23    标题: (求教)检测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 ]

作者: lg560852     时间: 2007-7-27 11:26
这是偶又调试时的情况
修改后:
::@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
请按任意键继续. . .

作者: lg560852     时间: 2007-7-27 11:28
这里为什么又调用了一次check

作者: wudixin96     时间: 2007-7-27 11:35
因为这里call :check %_input%了

call完后,就自动回到call的下一行了

作者: lg560852     时间: 2007-7-27 11:40
那应该怎么改呢??

作者: wudixin96     时间: 2007-7-27 11:42
call :check %_input%
goto end

作者: lg560852     时间: 2007-7-27 11:45
多谢达人指教!!