嗨,我正在研究一个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 /bHi 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 /bPros:
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 /bPros:
If you have a lot of variables to clear, this will save a ton of typing.Cons:
Variable values won't persist across multiple runs unless you store them somewhere更多推荐
发布评论