我编写了一个批处理文件,允许我通过输入详细信息来设置自定义ping请求,但是当我尝试运行它时,我可以输入IP地址而不会出现问题,如果我输入exit但是当我输入时它会退出数据包大小的数字在评估if %size% GTR 4096
时会引发意外错误。
我查看了其他帖子,例如this,据我所知,我的代码应该可行。这有什么问题?
:pinger_presub_error
color
echo Invalid input
set size = 64
pause > nul
:pinger
title Pinger
color 0B
cls
echo Enter exit at any point to quit.
echo.
echo Enter IP Adress to ping:
set /p IPAdress=
if "%IPAdress%" == "exit" goto end
echo.
echo Enter packet size 8-4096, leave empty for default(64):
set /p /a size=
if "%size%" == "exit" goto end
if "%size%" == "" set size = 64
if %size% GTR 4096 (goto pinger_presub_error)
if %size% LSS 8 (goto pinger_presub_error)
echo.
echo Enter number of echos 1-20, leave empty for default(1):
set /p /a echos=
if "%echos%" == "exit" goto end
if "%echos%" == "" set echo = 1
if %echos% GTR 20 (goto pinger_presub_error)
if %echos% LSS 1 (goto pinger_presub_error)
cls
echo [Ctrl]+[C] to end
ping /l %size% /t /n %echos% %IPAdress%
goto end
:end
exit
答案 0 :(得分:3)
set size = 64
将名为size<space>
的变量设置为值<space>64
删除空格(或添加/a
):
set "size=64"
或
set /a size = 64
答案 1 :(得分:2)
set /p /a size=
^..^...........one or the other.
使用/p
提示用户和/a
提示算术。您的代码询问用户,检索到的值存储在名为/a size
v .......space included in variable value
set size = 64
^ .........space included in variable name
更好地使用
set "size=64"
当然,在其余代码中更改指示的问题。您的echos
(也称为echo
)变量也存在同样的问题。