首页 > 解决方案 > 我如何提出一个问题,如果用户不输入任何内容,它就会回到问题?

问题描述

目前我只有这个

@echo off
title "title"
:A
set /p Name=Whats your name?
if /i "%answer:~,1%" EQU "" goto a
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
set /p still=whats the password yes or no? (Y,N) :
if /i "%answer:~,1%" EQU "Y" goto b
if /i "%answer:~,1%: EQU "N" exit /b
:b
echo secret file users/desktop/name/files

标签: batch-file

解决方案


这是您的用户提示演示批处理文件,其中包含用于安全处理用户输入的附加命令行。批处理文件包含解释代码的注释。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
title User prompt demo
cls

:GetName
rem Undefine the environment variable Name.
set "Name="

rem Prompt the user for the name.
set /P "Name=What is your name? "

rem Has the user just pressed RETURN or ENTER without entering a string?
if not defined Name goto GetName

rem Remove all double quotes from user input string.
set "Name=%Name:"=%"

rem Has the user entered just double quotes?
if not defined Name goto GetName

rem Output an empty line.
echo/

rem Enable delayed expansion and output the input string which could
rem contain characters like ampersands, angle brackets or pipes which
rem would modify the command line with ECHO before execution on not
rem using delayed environment variable expansion. The exclamation mark
rem at end must be escaped twice with caret character to be interpreted
rem as literal character to print by command ECHO in this case. Then
rem restore the previous execution environment as defined at top.
setlocal EnableDelayedExpansion
echo Hello !Name!^^!
endlocal

echo/
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo/

rem A classic yes/no prompt is done best with using command CHOICE which
rem is by default available since Windows Vista and Windows Server 2003.
%SystemRoot%\System32\choice.exe /C YN /N /M "What is the password, yes or no? (Y,N):"
if errorlevel 2 exit /B

rem The user pressed key Y and so the batch file processing continues.
echo/
echo Secret file: %UserProfile%\Desktop\name\files
endlocal

要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • choice /?
  • cls /?
  • echo /?
  • endlocal /?
  • exit /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?

也可以看看:

您可能还对如何在用户的桌面目录中创建目录感兴趣?它提供了一个带有几行的批处理文件,可以直接从 Windows 注册表中确定哪个目录是用户的桌面目录,因为这%UserProfile%\Desktop只是默认设置,每个用户都可以通过单击几下自由地将不同的目录用作桌面目录。


推荐阅读