我知道在这里必须使用“&”和“ &&”。但是GOTO呢?有标签或无标签时如何工作?我希望它看起来像例如:echo%time%&dir&echo%time%。
@echo off
:start
cls
echo -create (create password)
echo -check (check password)
echo.
set /p PROGRAM= What do you want to do?:
goto %PROGRAM%
:create
cls
set /p PASSWORD= What do you want your password to be?:
echo %PASSWORD% > test.txt
pause
goto start
:check
cls
set /p PASSWORD1= What is your password?
for /f "Delims=" %%a in (test.txt) do (
set TEXT=%%a
)
if %PASSWORD1%==%TEXT% goto correct
echo you are wrong!!!
pause
goto start
:correct
echo you are right!!!
pause
goto start
答案 0 :(得分:2)
不幸的是,正如已经提到的那样,不可能在命令行中使用标签。
在您所考虑的前提下,最好的选择是将每个例程包含在if / else
命令中,并将循环替换为无限循环。因此,您最终得到了这样的东西(“可读的”原型)
for /l %a in (0) do (
cls
& echo -create (create password)
& echo -check (check password)
& echo/
& set pass=
& set p=
& set /p p=What do you want to do?
& cls
& (if !p!==create (
set /p pass=What do you want your password to be?:
& (echo !pass!)>test.txt
) else if !p!==check (
set /p pass=What is your password?
& set /p text=<test.txt
& if !pass!==!text! (
echo You are right !!!
) else (
echo You are wrong !!!
)
) else (
echo wrong command !!!
))
& echo/ & pause
)
注意:
goto :start
现在是一个无限的for /l
循环。&
运算符。 if
或else
块中的第一个命令应该没有它。!var!
)。cmd
调用中包括一些开关现在,所有内容都排成一行:
cmd /e /v /c"for /l %a in (0) do ( cls & echo -create (create password) & echo -check (check password) & echo/ & set pass= & set p= & set /p p=What do you want to do? & cls & (if !p!==create ( set /p pass=What do you want your password to be?: & (echo !pass!)>test.txt ) else if !p!==check ( set /p pass=What is your password? & set /p text=<test.txt & if !pass!==!text! ( echo You are right !!! ) else ( echo You are wrong !!! ) ) else ( echo wrong command !!! )) & echo/ & pause )"
其中包含/e
(启用扩展名,通常已经启用,但以防万一),/v
(启用延迟扩展)开关,以确保新的cmd
实例是使用所需的配置。
无论如何,请记住,您可以编写的最长命令行为8191个字符长。
答案 1 :(得分:0)
这是我要这样做的方式:
for /L %a in () do (cls & echo 1- Create password & echo 2- Check password & echo/& choice /N /C 12 /M "What do you want to do? " & cls & ( if not errorlevel 2 (set /p "pass=What do you want your password to be?: " & (call echo %^pass%)>test.txt) else (set /p "pass=What is your password?: " & (call echo %^pass%)>text.txt & fc test.txt text.txt >nul & if not errorlevel 1 (echo You are right !!!) else (echo You are wrong !!!)) ) & echo/ & pause)
这是必须在命令行上放置的单行版本:
cmd.exe
要在cmd /C "for /L %a in () do ( ... pause)"
调用中执行此行,只需将引号之间的整个行括起来:
choice
使用if errorlevel
命令进行选项选择更为简单。除此之外,这样做还可以使用fc
格式,而不需要延迟扩展...通过{{1}}命令进行密码比较也有同样的要求。
在Windows 8.1上测试