如果用户什么都不输入,如何结束批处理文件(How to end batch file if the user enters nothing)

编程入门 行业动态 更新时间:2024-10-24 06:28:06
如果用户什么都不输入,如何结束批处理文件(How to end batch file if the user enters nothing)

嗨,我正在研究一个Windows批处理文件,如果用户没有输入字符串,我试图让程序结束,但是当我运行它并且不输入任何东西时,整个事情仍然运行。 任何建议都会非常感谢。

:: Sets variable studentName to what the user inputs. set /p studentName=Enter student name: ::If the user does not input anything go to end option if "%studentName%"=="" goto end :: Displays filename, student's entered name, and the random number echo Usage: %0 %studentName% echo Hello %studentName%, your secret number is %RANDOM% :: Pauses screen while user reads secret number pause :: Clear screen for user. cls echo Hope you remeber that number, %studentName%! :end echo Usage: %0 studentName pause exit /b

Hi I am working on a windows batch file and am trying to get the program to end if the user does not enter a string, but when I run it and do not input anything the whole thing still runs. Any advice would be great thanks.

:: Sets variable studentName to what the user inputs. set /p studentName=Enter student name: ::If the user does not input anything go to end option if "%studentName%"=="" goto end :: Displays filename, student's entered name, and the random number echo Usage: %0 %studentName% echo Hello %studentName%, your secret number is %RANDOM% :: Pauses screen while user reads secret number pause :: Clear screen for user. cls echo Hope you remeber that number, %studentName%! :end echo Usage: %0 studentName pause exit /b

最满意答案

在常规批处理脚本中设置变量时,它们会一直存在于环境中,直到它们被删除或环境关闭为止。 您的问题源于这样一个事实:您在没有先准备环境的情况下给%studentName%一个值。 你有两个选择:

选项1:在使用之前清除变量

@echo off :: Clears the value of %studentName%. The quotes are to prevent extra spaces from sneaking onto the end set "studentName=" :: Sets variable studentName to what the user inputs. set /p studentName=Enter student name: ::If the user does not input anything go to end option if "%studentName%"=="" goto end :: Displays filename, student's entered name, and the random number echo Usage: %0 %studentName% echo Hello %studentName%, your secret number is %RANDOM% :: Pauses screen while user reads secret number pause :: Clear screen for user. cls echo Hope you remeber that number, %studentName%! :end echo Usage: %0 studentName pause exit /b

优点:

如果你需要其他变量来保持,你可以反复运行它们,它们将保持不变直到命令提示符关闭。

缺点:

如果您有许多不需要保留的变量,则需要手动清除每个变量。

选项2:使用setlocal创建新环境

@echo off setlocal :: Sets variable studentName to what the user inputs. set /p studentName=Enter student name: ::If the user does not input anything go to end option if "%studentName%"=="" goto end :: Displays filename, student's entered name, and the random number echo Usage: %0 %studentName% echo Hello %studentName%, your secret number is %RANDOM% :: Pauses screen while user reads secret number pause :: Clear screen for user. cls echo Hope you remeber that number, %studentName%! :end echo Usage: %0 studentName pause exit /b

优点:

如果要清除很多变量,这将节省大量的输入。 如果需要使用延迟扩展时 ,无论如何都需要使用此方法

缺点:

除非将它们存储在某处,否则变量值不会在多次运行中持续存在

When variables are set in a normal batch script, they persist in the environment until they are deleted or the environment is closed. Your problem stems from the fact that you gave %studentName% a value without preparing the environment first. You have two options:

Option 1: Clear the variable before using it

@echo off :: Clears the value of %studentName%. The quotes are to prevent extra spaces from sneaking onto the end set "studentName=" :: Sets variable studentName to what the user inputs. set /p studentName=Enter student name: ::If the user does not input anything go to end option if "%studentName%"=="" goto end :: Displays filename, student's entered name, and the random number echo Usage: %0 %studentName% echo Hello %studentName%, your secret number is %RANDOM% :: Pauses screen while user reads secret number pause :: Clear screen for user. cls echo Hope you remeber that number, %studentName%! :end echo Usage: %0 studentName pause exit /b

Pros:

If you need other variables to persist, you can run this over and over and they will stay until the command prompt is closed.

Cons:

If you have a lot of variables that need to not persist, you need to clear each one manually.

Option 2: Using setlocal to create a new environment

@echo off setlocal :: Sets variable studentName to what the user inputs. set /p studentName=Enter student name: ::If the user does not input anything go to end option if "%studentName%"=="" goto end :: Displays filename, student's entered name, and the random number echo Usage: %0 %studentName% echo Hello %studentName%, your secret number is %RANDOM% :: Pauses screen while user reads secret number pause :: Clear screen for user. cls echo Hope you remeber that number, %studentName%! :end echo Usage: %0 studentName pause exit /b

Pros:

If you have a lot of variables to clear, this will save a ton of typing. If When you need to use delayed expansion, you'll need to use this method anyway

Cons:

Variable values won't persist across multiple runs unless you store them somewhere

更多推荐

本文发布于:2023-04-29 07:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么都不   批处理文件   结束   用户   user

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!