用户输入没有被采用?批处理文件

时间:2016-02-09 05:51:19

标签: batch-file cmd command-prompt

当我的一个学生遇到一个问题时,我已经为我的非官方编码类制作了这个代码,每当一个预期的答案被采取时,该过程就会结束任务。我一遍又一遍地检查他的代码,并且不知道问题出在哪里。

    @echo off
rem Everywhere you see "rem" as a command is simply developer notes to help keep track of what and where everything is. Deleting all rem commands will not affect the program.
if not exist "C:\Program Files\Led\Ping Testor\version\1.7.info" goto new
if exist "C:\Program Files\Led\Ping Testor\version\1.7.info" goto old
rem Checks for first-time launch on local machine.

:new
if not exist "C:\Program Files\Led"
(
cd "C:\Program Files\"
md Led
)
if not exist "C:\Program Files\Led\Ping Testor"
(
cd "C:\Program Files\Led"
md "Ping Testor"
)
if not exist "C:\Program Files\Led\Ping Testor\version"
(
cd "C:\Program Files\Led\Ping Testor\"
md "version"
)
if not exist "C:\Program Files\Led\Ping Testor\version\1.7.info"
(
cd "C:\Program Files\Led\Ping Testor\version"
echo "1.7235 Release. Beta Officially Closed. More Information at LedDevelopment.CU.CC/main/products/led-ping-testor" >1.7.info
)
goto old
rem Creates all necessary directories if they don't exist and adds version information to the directory.

:old
title Led Development AB 2015 - 2016
color 0a
mode con: cols=65 lines=18
cls
rem Window setup (Initial).

:a
echo Led Ping Testor                                        v1.7235(R)   
rem The (R) after the version number indicates state/build type. (R) in this case means release, as in finished product. (B) is for Beta (Used for testing and developing by both users and proffessional developers), and there's (A) which is the essentials of the code for the program to function at all(Led Development Team Only).Occasionally, an (E) (evaluation copy) is developed.
echo ===============                                        ==========
echo Welcome to the Led Ping Testor. Please choose 1 to test your pingto our official website server or 2 to test your connection to a custom IP Address/Website. If you don't understand any of this,  choose option 1.
echo.
echo NOTE: A log file keeps track of all the ping results,as well as  time and date,but no personal information is kept,including your IP Address or computer name. To view the log file,type "log"     without the quotation marks and hit enter. Similarly,if you wish to clear the log of all data,type "logclr" without the quotation marks and hit enter.
rem Intro text.

set /p op=
if %op%== 1 goto ping1
if %op%== 2 goto ping2a
if %op%== log goto log
if %op%== logclr goto logclr
rem This checks for all eligible user input for this stage.
goto old
rem This takes user back to Main Screen in case of non-eligible input.

:log
cls
if exist "C:\Program Files\Led\Ping Testor\log.txt"
(
cd "C:\Program Files\Led\Ping Testor\"
start log.txt
)
rem Opens log if exists.

if not exist "C:\Program Files\Led\Ping Testor\log.txt"
(
cls
echo The log file is empty, thus cannot be openned.
pause
goto a
)
rem Gives error message if log doesn't exist.


:logclr
cls
cd "C:\Program Files\Led\Ping Testor\"
del log.txt
goto old

:ping1
echo Please choose how many times you want to ping the host.
set /p %numb%
if %numb% < 1 goto ping1a
if %numb% > 99999 goto ping1a
rem Checks if user input was between 1 and 99999 to confirm user input as an interger (numbers only).
cls
rem If user input isn't an integer between 1 and 99999, error message explaining appears in a new  window.
msg %username% Warning: Not a numerical value between 1 and 99999. Please check your spelling and remove all letters, special characters and spaces.
goto ping1

:ping1a
cls
title Pinging 185.28.21.39...
cls
cd "C:\Program Files\Led\Ping Testor\"
echo ========================================================= >>log.txt
echo Host:185.28.21.39          Ping Count:%numb% >>log.txt
echo Include this in any bug reports for the Ping Testor. >>log.txt
echo %date% >> log.txt
rem All this chunck of code above is the injection of data to the log file.

:ping11
echo Led Ping Testor
echo ===============
echo The ping is called time, and is displayed upon every loop.
echo.
ping 185.28.21.39 -n %numb% >>log.txt
ping 185.28.21.39 -n %numb%
ping localhost -n 2 >nul
cls
goto ping11
rem And this chunck performs the same thing as above but infront of the user.

问题在于:

echo Welcome to the Led Ping Testor. Please choose 1 to test your pingto our official website server or 2 to test your connection to a custom IP Address/Website. If you don't understand any of this,  choose option 1.
echo.
echo NOTE: A log file keeps track of all the ping results,as well as  time and date,but no personal information is kept,including your IP Address or computer name. To view the log file,type "log"     without the quotation marks and hit enter. Similarly,if you wish to clear the log of all data,type "logclr" without the quotation marks and hit enter.
rem Intro text.

set /p op=
if %op%== 1 goto ping1
if %op%== 2 goto ping2a
if %op%== log goto log
if %op%== logclr goto logclr
rem This checks for all eligible user input for this stage.
goto old
rem This takes user back to Main Screen in case of non-eligible input.

是的,我知道:ping2仍然不存在,因为我还没有将它添加到此代码中,但现有的命令都没有,比如ping1(通过按1),log和logclr。 Logclr,有时,似乎有点&#34;刷新&#34;屏幕。 感谢您的帮助。我会将这个人的名字添加到某个地方的学分中。也许在网站上(leddevelopment.cu.cc/main/products/led-ping-testor /)。

1 个答案:

答案 0 :(得分:1)

不清楚你的意思&#34;工作&#34;。第二段代码&#34;为我工作 - 也就是说,按照我的预期做了。

由于您尚未调用setlocal,因此在早期运行期间进行的任何设置都将保留在环境中。也许这是一个原因。

如果你使用if "%var%"=="string" ...,那么用户进入空格会再次受到保护。您也可以尝试使用if /i ...来区分大小写。

您的if %numb% < 1 goto ping1a无效,<>是重定向器。您需要分别使用lssgtr

如果这些线索无法解决您的问题,请说明发生了什么以及实际发生了什么。