2014-07-22 3 views
1

저는 일괄 처리에 익숙하지 않고 프로젝트에 대해 "두뇌처럼"프로그램을 만들려고 노력합니다. 간단한 짧은 대화를 완료 할 수 있어야합니다. 나는 다음과 같이 사용자의 질문을 설정/P를 사용하고 있습니다 :하위 문자열을 일괄 적으로 검색

set /p a= Hello: 

내가 사용자가 자신의 대답에 특정 단어가 컴퓨터가 응답 할 것이다 결정하는 데 도움라고하면 볼 수 있어야합니다.

감사합니다.

답변

1

IF 일괄 처리에는 기본 하위 문자열 펑크가 없으므로 도움이되지 않습니다.

set a=user inputted something with a word in it. 
echo %a%|find /i "word" >nul && (echo there is "word" in the input) 

/i 그것이

>nulfind 경우였다 "로 화면에

&& 행위를 그것의 결과를 표시하지 않도록를 알려줍니다 capitalzation을 무시하도록 지시 :하지만 당신은 약간의 트릭을 에뮬레이션 할 수 있습니다 성공적인, 다음 ... "

2
@echo off 
set "specific_word=something" 

set /p a= Hello: 


setlocal EnableDelayedExpansion 
if /I not "!a:%specific_word%=!" EQU "!a!" (
    echo it contains the word 
) else (
    echo it does not contain the word 
) 


echo %a%|find /i "%specific_word%" >nul 2>&1 

echo --OR-- 

if errorlevel 1 (
    echo it does not contain the word 

) else (
    echo it contains the word 
) 

IF 방법 가 더 빠르다.

2

방탄 코드가 아니라 단지 해골입니다. "단어"를 보장하기 위해 필요한 경우

@echo off 

    setlocal enableextensions disabledelayedexpansion 

:input 
    set "typed=" 
    set /p "typed=what? " 
    if not defined typed goto :input 

    rem Option 1 - Use find 
    echo(%typed% | find /i " word " >nul 
    if not errorlevel 1 echo FIND : "word" has been used 

    rem Option 2 - Use substring replacement 
    set "text= %typed% " 
    if not "%text: word =%"=="%text%" (
     echo IF : "word" has been used 
    ) 

    rem Option 3 - Tokenize the input 
    set "text=%typed:"= %" 
    for %%a in ("%text: =" "%") do (
     if /i "%%~a"=="word" echo FOR : "word" has been used 
    ) 

    endlocal 

이 추가적으로 공간이 추가되는 입력 된 텍스트에서 "단어"단어의 존재에 대한이 코드 검사는 "칼"내부에서 찾을 수 없습니다.

1

는 find 명령은 매우 우아하지

사용할 수 있지만 당신은 FIND 명령과 if 문 시리즈를 사용할 수 있습니다.

@echo off 
set /p a= "Hello: " 

echo %a% | C:\Windows\System32\FIND /I "Hi" > nul 2>&1 
set FIND_RC_0=%ERRORLEVEL% 

echo %a% | C:\Windows\System32\FIND /I "Howdy" > nul 2>&1 
set FIND_RC_1=%ERRORLEVEL% 

if "%FIND_RC_0%" == "0" (
    set /p b= "How are you today?: " 
) 

if "%FIND_RC_1%" == "0" (
    set /p b= "How you doing partner?: " 
) 
관련 문제