Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 包含在集合中的变量_Windows_Batch File_If Statement - Fatal编程技术网

Windows 包含在集合中的变量

Windows 包含在集合中的变量,windows,batch-file,if-statement,Windows,Batch File,If Statement,IF命令有一种方法可以在一组值中包含一个变量 我的意思是: 如果%%i输入(abc 123 opl)回显第一组 如果%%i英寸(xyz 456 bnm)回波第二组 您可以使用for语句来执行此操作。下面是一个脚本,您可以使用以下内容运行它: myprog 456 它将在集合2中输出: @setlocal enableextensions enabledelayedexpansion @echo off for %%a in (abc 123 opl) do ( if "x%%a"=="

IF命令有一种方法可以在一组值中包含一个变量

我的意思是:

如果%%i输入(abc 123 opl)回显第一组

如果%%i英寸(xyz 456 bnm)回波第二组


您可以使用
for
语句来执行此操作。下面是一个脚本,您可以使用以下内容运行它:

myprog 456
它将在集合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
从命令行:

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

而且,您也不局限于在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

我看到它可以工作,但我想将变量从批处理文件中传递到for循环,因此我想在代码的开头添加%%a=123。您编写的命令是一个“带修饰符的变量”?然后您只需在脚本开头添加类似于
set xx=7
的内容,并使用
%xx%
而不是
%1
。非常感谢。您使用的参数是FOR命令的修饰符,我试图在windows帮助中查找它,但没有找到。我现在了解到%1是在dos提示下提供的参数,很抱歉