
set /p codename="Please enter the codename! "
if exist %codename% = Candy, Sugar, Lollipop (
echo The code you entered is not available.
)
사용자가 %codename%을 Candy, Sugar 또는 Lollipop 등으로 입력하는 경우 "입력한 코드는 사용할 수 없습니다."라는 에코 메시지를 표시한 다음 박쥐를 일시 중지할 수 있는 방법이 있습니까?
사용자가 이 세 가지를 제외한 다른 항목을 입력하면 스크립트가 계속 작동합니다.
어떻게 해야 하나요?
답변1
여러 문자열을 사용하여 사용자 입력의 유효성을 검사할 수 있습니다.findstr
:
echo/%codename% |%__APPDIR__%findstr.exe "Candy Sugar Lollipop" >nul && goto :Next
1)echo/%codename%
(문자열 입력) ~findstr
(문자열 찾기),
2) 사용/i
대소문자를 구분하지 않음필요하다면 제거하고, 그렇지 않다면 제거하세요./i
3) 일치하는 경우,goto :next
label... 그렇지 않은 경우 다음 줄을 실행합니다...
4) 에코/메시지 및 시간 초과가 사용자에게 표시되고 사용자가 아무 키나 누를 때까지 기다립니다.
5)goto :EOF
(파일 끝), 박쥐 종료/중단/종료와 동일합니다.
@echo off & setlocal
set /p codename="Please enter the codename! "
echo/%codename% |%__APPDIR__%findstr.exe "Candy Sugar Lollipop" >nul && goto :Next
echo/The code you entered is not available^!! & %__APPDIR__%timeout -1 & goto :EOF
:Next
rem :: your code continue here...`
- 관찰사용자 입력만 입력하는 경우 이를 다음으로 제한할 수 있습니다.
if !_cnt! equ 3
@echo off & setlocal EnableDelayedExpansion
:loop
set /p codename="Please enter the codename! "
set /a "_cnt+=1+0"
if !_cnt! equ 3 (
echo/Maximum number of attempts allowed exceeded^!!
goto :Error ) else if "!codename!"=="" goto :loop
echo/!codename! |%__APPDIR__%findstr.exe "Candy Sugar Lollipop" >nul && goto :Next
:Error
echo/The code you entered is not available^!! & %__APPDIR__%timeout -1
endlocal & goto :EOF
:Next
rem :: your code continue here...
답변2
존재하는 경우 변수 내용에 대한 잘못된 비교 형식입니다. 올바른 형식은 다음과 같습니다.
If "%variable%"=="desired value" (command)
지정된 허용 값에 대해 동일한 명령을 실행하려면 다음과 같이 for 루프를 사용할 수 있습니다.
For %%A In (Candy,Sugar,Lollipop) Do (
If /I "%codename%"=="%%A" (
Call :CodeTrue
)
)
스크립트 끝의 함수로
:CodeTrue
Pause
Rem other commands
Exit /b
답변3
PowerShell로 이동할 수 있는 경우:
$NewName = Read-host -Prompt 'Please enter the codename!'
If ($NewName -in ('Candy', 'Sugar', 'Lollipop') ) { echo 'The code you entered is not available.'}
PS C:\> $NewName = Read-host -Prompt 'Please enter the codename!'
>> If ($NewName -in ('Candy', 'Sugar', 'Lollipop') ) { echo 'The code you entered is not available.'}
Please enter the codename!: Candy
The code you entered is not available.
PS C:\>
진정한 환경 변수가 필요합니까?
$NewName = Read-host -Prompt 'Please enter the codename!'
If ($NewName -in ('Candy', 'Sugar', 'Lollipop') ) {
echo 'The code you entered is not available.'
}
Else {$env:CodeName = $NewName} # only exists within scope of procesd
or
Else {[Environment]::SetEnvironmentVariable("CodeName", $NewName, "User")} # Persistant at user-level
or (Requires Admin PowerShell Console. Not avaiable to creating process.)
Else {[Environment]::SetEnvironmentVariable("CodeName", $NewName, "Machine")} # Persistant at machine-level