批处理文件。如果用户输入的东西弄清楚它是哪一个

时间:2015-10-16 18:32:41

标签: batch-file runtime-error

所以我正在创建一个打开特定信息的批处理文件,比如我想在我的批处理文件中打开需要用户输入的信息。如File1,File2& file3的。

如果用户在用户输入区域中输入File2,我希望将其转到它,这是我第一次尝试代码。

:loggedin
echo Access Granted
echo Security Level 1
set /p select=Enter Password:%=%
::Checks the file the user is looking for
if "%select%"=="" (
cls
echo !NOTHING WAS INPUT INTO THE CONSOLE TRY AGAIN!
pause
goto :loggedin
) else ( 
if "select"=="file1"(
::User has input the file name. Show the content.
echo "Content of file 1"
) else (
if "select"=="file2"(
echo "Content of file2"
) else (
if "select"=="file3"(
echo "content of file three"
) else (
goto :loggedin

我知道这是非常错误的代码,但我很难绕过它,我尝试了多种方法,仍然无法超越它。

我会使用ERRORLEVEL来解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

变量必须附加%!(如果是延迟扩展)。您还可以使用/I切换IF。而且没有别的 - 如果在bacth文件中构建。在括号块中,使用REM作为注释更安全:

:loggedin
echo Access Granted
echo Security Level 1
set /p select=Enter Password:%=%
::Checks the file the user is looking for
if "%select%"=="" (
 cls
 echo !NOTHING WAS INPUT INTO THE CONSOLE TRY AGAIN!
 pause
 goto :loggedin
) 

if /I "%select%"=="file1" (
 rem User has input the file name. Show the content.
 echo "Content of file 1"
) 

if /I "%select%"=="file2" (
 echo "Content of file2"
) 

if /i "%select%"=="file3"(
 echo "content of file three"
) 
goto :loggedin

答案 1 :(得分:1)

else有时很有用,但您在此处不需要它:

:loggedin
echo Access Granted
echo Security Level 1
set /p select=Enter Password:%=%
::Checks the file the user is looking for
if "%select%"=="" (
  cls
  echo !NOTHING WAS INPUT INTO THE CONSOLE TRY AGAIN!
  pause
)
if /i "%select%"=="file1" (
  ::User has input the file name. Show the content.
  echo "Content of file 1"
)
if /i "%select%"=="file2" (
  echo "Content of file2"
)
if /i "%select%"=="file3" (
  echo "content of file three"
)
goto :loggedin

另外:它说"访问授权"然后要求您输入密码

我为/i添加了if,以使其不区分大小写。