批处理文件显示提示不正确 [英] batch file display prompt not correct

查看:104
本文介绍了批处理文件显示提示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有与批量低于两个问题。第一个是,当打开时批文件提示用户用是/否的问题:

问题1

 的检查已经完成

如果这个问题的答案是Y,然后又一个Y / N显示问题

问题2

 您要发送的剂量报告

如果对问题1的答案是n的检查功能被调用并显示另一个问题。然而,目前正在以粗体显示的行,然后它会在第二部分(再见功能)。我究竟做错了什么?谢谢:)

当前批次

  @ECHO OFF
::询问用户
 :选择
 集/ p C =已检查已经完成[Y / N]
 如果/ I%C%== Y(
 设置/ p C =你要发送的剂量报告[Y / N]?
 )其他(
 如果/ I%C%==ñ转到支票
 )
 如果/ I%C%== Y(
L:\\ NGS \\ HLA LAB \\全面质量​​管理\\ QC&放大器; QA \\剂量报告\\报告剂量form.xlsm
 )其他(
 如果/ I%C%==ñ转到再见
 )
 :检查
 集/ p C =你要执行的检查[Y / N]
 如果/ I%C%== Y(
 回声进行检查,并按下回车键时,完成
 暂停后藤的选择
 )其他(
 如果/ I%C%==ñ转到再见
 ::计数循环
 设置VAR1 = 0
 :循环
 集/ A VAR1 =%VAR1%+ 1 回声%VAR1% 如果VAR1%EQU%1(
 转到结束
 )其他(
 转到循环
 )
 :结束
 回声剂量报告已在%DATE%在%TIME%通过发送%USERNAME%
 :再见
 回声再见
 TIMEOUT 2 / NOBREAK
 出口


解决方案

首先,在的if-else语句:选择缺少其结束

继承人用户输入一个重要的注意,永远不要相信它。尝试把除y以外的东西/ N在第一个

 :选择
集/ p C =已检查已经完成[Y / N]
 如果/ I%C%== Y(
   设置/ p C =你要发送的剂量报告[Y / N]?
  )其他(
     如果/ I%C%==ñ转到支票

如果你输入一些非法进入第一次的if-else,说有人试图键入没有而不是 N ,它将无法将它们返还给:选择,因为它仅检查 N

和最终通过脚本运行。在你的情况下,失败的if语句前:检查并启动:检查的proccess,但在检查同样的问题出现时,它会通过运行 ::计数循环键,下面的命令在那里可以弄乱你的数据。

每个if-else语句后,它非常安全的做法是添加一个默认的动作,如;

 :选择
集/ p C =已检查已经完成[Y / N]
 如果/ I%C%== Y(
   设置/ p C =你要发送的剂量报告[Y / N]?
  )其他(
    如果/ I%C%==ñ转到支票
):如果两个if语句是假的,它会达到这一行:]
CLS
回声错误:%C%不是Y / N。
暂停
转到:选择

另一件事要注意,如果不输入,将发生错误。您可以通过该变量在之后定义设置/ pç检查解​​决这个问题=已检查已经完成[Y / N] 使用:

 如果没有定义C(
   CLS
   回声错误:输入不能为空!
   暂停
   转到:选择

所以,首先要做的检查的正确方法应该是:

 :选择
集/ p C =已检查已经完成[Y / N]
:[清空检查]
如果没有定义ÇCLS&安培;回声错误:输入不能为空! &安培;暂停&安培;转到:选择
 如果/我%C%== Y(
   设置/ p C =你要发送的剂量报告[Y / N]?
  )其他(
    如果/我%C%== N转到支票
):[ 输入无效 ]
CLS&安培;回声错误:%C%不是Y / N。 &安培;暂停&安培;转到:选择

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:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE 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

解决方案

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

这篇关于批处理文件显示提示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆