2010-10-18 77 views
0

IF命令有一种方法将变量包含在一组值中?集合中包含的变量

我的意思是:

IF %% i的(ABC 123 OPL)回声首先设置

IF %% i的(XYZ 456 BNM)回声第二设定

回答

0

C:\Users\preet>set val=99 
C:\Users\preet>for %f in (100 99 21) do @if (%f)==(%val%) echo found it %f 
found it 99 

在一个批处理文件

set val=99 
for %%f in (100 99 21) do @if (%%f)==(%val%) echo found it %%f 
+0

好的谢谢,那是我更喜欢的简单方式 – aemme 2010-10-18 09:31:14

0

您可以使用for声明来做到这一点。这里有一个脚本,它可以让你喜欢的东西运行:

myprog 456 

,它会输出in set 2:在命令行

@setlocal enableextensions enabledelayedexpansion 
@echo off 
for %%a in (abc 123 opl) do (
    if "x%%a"=="x%1" echo in set 1 
) 
for %%a in (xyz 456 bnm) do (
    if "x%%a"=="x%1" echo in set 2 
) 
@endlocal 
+0

我看到了它的工作原理,但我想给变量从批处理文件中传递给for循环,所以我想放%% a = 123在代码的开头。你写的命令是“带修饰符的变量”? – aemme 2010-10-18 09:18:20

+0

然后,您只需在脚本的开头放置'set xx = 7',并使用'%xx%'而不是'%1'。 – paxdiablo 2010-10-18 09:56:31

+0

非常感谢。您使用的参数是FOR命令的修饰符,我试图在Windows帮助中查找它,但我没有找到它。 – aemme 2010-10-18 10:08:51

0

而且您也不仅限于在Windows计算机中进行批处理。还有vbscript(和powershell)。这里是你如何检查使用VBScript

strVar = WScript.Arguments(0) 
Set dictfirst = CreateObject("Scripting.Dictionary") 
Set dictsecond = CreateObject("Scripting.Dictionary") 
dictfirst.Add "abc",1 
dictfirst.Add "123",1 
dictfirst.Add "opl",1 
dictsecond.Add "xyz",1 
dictsecond.Add "456",1 
dictsecond.Add "bnm",1 
If dictfirst.Exists(strVar) Then 
    WScript.Echo strVar & " exists in first set" 
ElseIf dictsecond.Exists(strVar) Then 
    WScript.Echo strVar & " exists in second set" 
Else 
    WScript.Echo strVar & " doesn't exists in either sets" 
End If 

用法:

C:\test>cscript //nologo test.vbs abc 
abc exists in first set 

C:\test>cscript //nologo test.vbs xyz 
xyz exists in second set 

C:\test>cscript //nologo test.vbs peter 
peter doesn't exists in either sets