如何检查多个变量是否包含相同的单词? - 批量

时间:2015-01-17 07:05:20

标签: batch-file

如果我有多个vars ......

%v1%  
%v2%  
%v3%  

我想检查所有变量是否包含特定单词。如果所有变量DO实际上都有该单词,则执行a     转到,反之亦然。

5 个答案:

答案 0 :(得分:0)

@echo off

setlocal
rem word to check for
set word=word

rem variables
set var1=word1
rem set var=now0rd
set var2=word2
set var3=word3

setlocal EnableDelayedExpansion 
set checker=yes
rem list all your variables here
for %%a in  (var1 var2 var3 var) do (
    if defined %%a (
        set tmpvar=!%%a!
        rem echo "!tmpvar:%word%=!" NEQ "!tmpvar!"
        if "!tmpvar:%word%=!" EQU "!tmpvar!" (
            set checker=no
        )
    )

)
endlocal && (
    set checker=%checker%
)

rem remove the REMs to activate goto
if %checker% EQU yes (
    echo all variables contain %word%
    rem goto :somewhere
) else (
    echo at least one variable does not contain %word%
    rem goto :somewhere_else
)
endlocal

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL
SET "checkfor=someword"
FOR %%a IN (word1 word2 word3) DO SET "%%a=%checkfor%"
SET "word4=anotherword"
CALL :check
CALL :check2
ECHO ================
SET "word4=%checkfor%"
CALL :check
CALL :check2
ECHO ================
SET "word3="
CALL :check
CALL :check2
ECHO ================
GOTO :EOF

:: Check that all contain "someword"
:check
SET word
FOR %%a IN (word1 word2 word3 word4) DO (
 FOR /f "tokens=1*delims==" %%b IN ('set 2^>nul') DO IF /i "%%a"=="%%b" IF /i "%%c" neq "%checkfor%" GOTO notsame1
)
ECHO all vars contain "%checkfor%"
GOTO :eof
:notsame1
ECHO NOT all vars contain "%checkfor%"
GOTO :eof

:: Check that all contain "someword"
:check2
FOR %%a IN (word1 word2 word3 word4) DO (
 SET "$found="
 FOR /f "tokens=1*delims==" %%b IN ('set 2^>nul') DO IF /i "%%a"=="%%b" SET "$found=Y"&IF /i "%%c" neq "%checkfor%" GOTO notsame2
 IF NOT DEFINED $found GOTO notsame2
)
ECHO all vars contain "%checkfor%"
GOTO :eof
:notsame2
ECHO NOT all vars contain "%checkfor%"
GOTO :eof

这里有两种方法。

首先检查变量是否(非)相同如果已定义。第二个检查它们全部定义并且全部与目标相同

天生地,你可以使用

set "myvars=word1 word2 word3 word4"

然后使用

FOR %%a IN (%myvars%) DO (

如果这样更方便(如果将此检查设置为子程序,可能会更方便)

答案 2 :(得分:0)

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem The values
    set "v1=this is a test"
    set "v2=a test this is"
    set "v3=this a test is"
    set "v4=this is a toast"

    rem What to search and where to search
    set "word=test"
    set "vars="v1" "v2" "v3" "v4""

    rem Count the number of variables
    set "numberOfVars=0"
    for %%a in (%vars%) do set /a "numberOfVars+=1"

    rem Count the number of variables containing the searched word
    set "occurences=0"
    for /f %%a in ('
        cmd /q /v /c "for %%a in (%vars%) do echo(!%%~a!" 
        ^| find /c "%word%"
    ') do set "occurrences=%%a"

    if %numberOfVars% equ %occurrences% (
        echo match 
    ) else (
        echo no match 
    )

答案 3 :(得分:0)

如果搜索词只能在每个变量中出现一次,那么下面的方法是解决此问题的最快方法,因为它基于迭代 for for each variable,但是在一个表达式中,无论变量的数量是多少,只计算一次。

@echo off
setlocal EnableDelayedExpansion



rem Define variables
set "v1=this is a test"
set "v2=a test this is"
set "v3=this a test is"
set "v4=this is a toast"

rem Define variables to search
set "vars=v1 v2 v3 v4"

rem Define a character that does NOT appear in the variables
set "delimiter=|"



rem Count the number of variables plus one
set tokens=1
for %%a in (%vars%) do set /A tokens+=1

rem Assemble the search expression
set "expr= ^!%vars: =^! ^!%^! "



rem Example: search for word "test"
set "word=test"
set "values=%expr%"
for /F "tokens=%tokens% delims=%delimiter%" %%a in ("!values:%word%=%delimiter%!") do (
   echo The word %word% is in all variables
   goto nextTest1
)
echo The word %word% is NOT in all variables...

:nextTest1
rem Example: search for word "this"
set "word=this"
set "values=%expr%"
for /F "tokens=%tokens% delims=%delimiter%" %%a in ("!values:%word%=%delimiter%!") do (
   echo The word %word% is in all variables
   goto nextTest2
)
echo The word %word% is NOT in all variables...

:nextTest2
rem Example: modify v4 and search again for "test"
set "v4=this finally also was a test"
set "word=test"
set "values=%expr%"
for /F "tokens=%tokens% delims=%delimiter%" %%a in ("!values:%word%=%delimiter%!") do (
   echo The word %word% is in all variables
   goto :EOF
)
echo The word %word% is NOT in all variables...

答案 4 :(得分:0)

给定"每个变量都包含一个字符串"解释,

@ECHO OFF
SETLOCAL

SET "Word1=Magic Sword"
SET "Word2=Magic Boots"
SET "Word3=Book of Magic Spells"
SET "Word4=Magic Underwear"

CALL :checkfor "Magic" "word1 word2 word3 word4"
ECHO =============================================
SET "Word3=Boring Book of Eggplant Recipes"

CALL :checkfor "Magic" "word1 word2 word3 word4"
ECHO =============================================

SET "Word3="

CALL :checkfor "Magic" "word1 word2 word3 word4"
ECHO =============================================

GOTO :EOF

:: Check that "%1" appears in each variable "%2"
:checkfor
ECHO check FOR "%~1" IN %~2
SET word

SETLOCAL enabledelayedexpansion
SET "vars= %~2 "

FOR /f "delims==" %%a IN ('set^|findstr /c:"%~1"') DO SET "vars=!vars: %%a = !
IF "%vars: =%"=="" ECHO "%~1" found IN each of "%~2"&GOTO :EOF 
FOR %%a IN (%vars%) DO IF DEFINED %%a ECHO "%~1" NOT found IN "%%a"&GOTO :EOF
ECHO all vars contain "%~1" but %vars% undefined
GOTO :eof