Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

batch file display prompt not correct

There are two issues with the batch below. The first is, when batch file when opened prompts the user with a "y/n" question:

Question 1

Has the check been done

If the answer to this is "y" then another "y/n" question is displayed

Question 2

Do you want to send the DOSE report

If the answer to question 1 is "n" the check function is called and another question is displayed. However, currently the line in bold is being displayed then it is going to the second part (the goodbye function). What am I doing wrong? Thank you :).

Current batch

@ECHO OFF
:: ask user 
 :choice
 set /P c=Has the check been done [y/n]
 if /i %c%==y (
 set /P c=Do you want to send the DOSE report[y/n]?
 ) else (
 if /i %c%==n goto check
 )
 if /i %c%==y ( 
"L:NGSHLA LAB	otal quality managementQC & QADOSE reportsDOSE reporting form.xlsm"
 ) else (
 if /i %c%==n goto goodbye
 )
 :check
 set /P c=Do you want to perform the check [y/n]
 if /i %c%==y (
 echo "perform check and hit enter when complete" 
 pause goto choice
 )    else (
 if /i %c%==n goto goodbye
 :: count loop
 set var1=0
 :loop
 set /a var1=%var1%+1

 echo %var1%

 if %var1% EQU 1 (
 goto end
 ) else (
 goto loop
 )
 :end
 echo "the DOSE report has already been sent by %USERNAME% on %DATE% at   %TIME%"
 :goodbye
 echo "goodbye"
 TIMEOUT 2 /nobreak
 exit
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First off, the if-else statement in :choice is missing its closing )

Heres an important note on user input, never trust it. Try putting something other than y/n in the first

:choice
set /p c=Has the check been done [y/n]
 if /i %c%==y (
   set /p c=Do you want to send the DOSE report[y/n]?
  ) else (
     if /i %c%==n goto check
)

If you input something invalid into the first if-else , say somebody tries typing no instead of n, it will fail to return them to :choice as it only checks for n or y

And end up running through script. In your case it fails the if statements before :check and starts :check's proccess, but in check the same issue arises, and it will run through to ::count loop and the following commands where it can mess up your data.

After each if-else statement, its VERY safe practice to add a default action, such as;

:choice
set /p c=Has the check been done [y/n]
 if /i %c%==y (
   set /p c=Do you want to send the DOSE report[y/n]?
  ) else (
    if /i %c%==n goto check
)

:[ If both if statements are false, it will reach this line: ]
cls
echo Error: "%c%" is not y/n.
pause
goto :choice

Another thing to note, if nothing is input, errors will occur. You can fix this by checking if the variable is defined right after set /p c=Has the check been done [y/n] using:

 if not defined c (
   cls
   echo Error: Input cannot be empty!
   pause
   goto :choice
)

So a proper way to do the first check would be:

:choice
set /p c=Has the check been done [y/n]
:[Empty check ]
if not defined c cls & echo Error: Input cannot be empty! & pause & goto :choice
 if /i "%c%==y" (
   set /p c=Do you want to send the DOSE report[y/n]?
  ) else (
    if /i "%c%==n" goto check
)

:[ Invalid input ]
cls & echo Error: "%c%" is not y/n. & pause & goto :choice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...