【问题标题】:Batch File If Error批处理文件如果错误
【发布时间】:2013-04-09 23:07:25
【问题描述】:

好的,我正在做一个项目,我正在做这件事,我在 confirmOne 中找到了 if 语句,它给了我“(此时不是预期的。”请帮忙!

许多流浪的“你到了这里!”消息来自我试图调试它。我真的很快就需要这个。请帮忙。我也尝试删除部分,但它似乎仍然不起作用。如果您发现任何其他错误,请告诉我,因为我需要我能得到的所有帮助。谢谢!

:grabInput
echo Please enter the username of the user you wish to access.

REM - } End Echoing Information/Main Menu | Grab Input {

set /p result=
goto correctName

REM - } End Grab Input | Process Input {

:correctName
set /p input=%result%
goto confirmOne
:confirmOne
echo Got to confirmOne
pause
if %input%==[] (
  pause
  cls
  echo Oops! Looks like you didn't enter anything! Try Agian!
  echo.
  echo ................................................................................
  echo.
  goto grabInput
) ELSE (
  goto confirmTwo
)


:confirmTwo
echo Got to ConfirmTwo
pause
if %input%==~help (
  goto helpMenu
) ELSE (
  goto confirmThree
)

:confirmThree
echo Got to ConfirmThree
if %input%==~info (
  goto infoMenu
) ELSE (
  goto swapDrive
)

【问题讨论】:

    标签: if-statement batch-file cmd batch-processing


    【解决方案1】:

    好吧,如果您没有为%input% 输入任何内容,那么您的if 语句将类似于if ==[] (

    您的if 语句应类似于if [%input%] == [] (

    我也看到了很多不必要的代码,你应该看看你的脚本。

    【讨论】:

      【解决方案2】:

      批处理总是适用于字符串。

      使用声明 if %input%==[],当 %input% 设置为 [nothing](这是您尝试检测的内容)时,批量替换 [nothing] fo %input% 并得到

      IF ==[] (
      

      因为'('不是比较运算符而感到困惑。

      [] 不是什么神奇的咒语。这是一种检测参数是否存在的旧方法,如果参数不存在,[%1] 将等于 []。当变量包含空格或某些其他字符时,它不起作用。

      if "%var%"=="" is better
      if not defined var is better still
      

      注意

      set /p var=
      

      不会将 var 设置为 [nothing],您只需按 enter,它将保持 var 不变。

      因此

      set var=something
      set /p var=
      

      会将var 设置为something。您应该将其编码为

      set "var="
      set /p var="Some prompt "
      if not defined var echo VAR is not defined
      

      var= 周围的引号可确保var 在行尾有空格时不会设置为[某些空格]。

      除此之外,顺序

      goto somelabel
      :somelabel
      

      (REM 行无关)是多余的。

      同样,在

      if somecondition (goto somewhere) else (goto somewhereelse)
      :somewhereelse
      

      else 条件是多余的

      Batch 仅将 :label 通知为 GOTOCALL 的 DESTINATION。否则将直接通过它发现的任何:label 收费,就好像它们是备注声明一样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-12
        • 2018-04-21
        • 2017-10-26
        • 2022-01-05
        • 1970-01-01
        • 2016-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多