如何找到一个字符串是在字符串中的一个DOS批处理文件列表 [英] How to find if a string is in a list of strings in a DOS batch file

查看:139
本文介绍了如何找到一个字符串是在字符串中的一个DOS批处理文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,如果基于字符串列表上的参数传递给一个批处理文件是有效的。

I'd like to check if an argument to a batch file is valid based on a list of strings.

例如:

IF %1 IN validArgument1, validArgument2, validArgument3 SET ARG=%1

这将设置为ARG的,只有当它匹配了有效的论据之一。理想的情况是不区分大小写的情况下

This would set ARG to one of the valid arguments only if it matched. Ideally case insensitively.

推荐答案

您也可以使用的阵列的办法:

You may also use the array approach:

setlocal EnableDelayedExpansion

set arg[1]=validArgument1
set arg[2]=validArgument2
set arg[3]=validArgument3

for /L %%i in (1,1,3) do if /I "%1" equ "!arg[%%i]!" SET "ARG=!arg[%%i]!"

在我看来,这种方法更清晰,更简单的管理与多个选项。例如,您可以创建的有效参数数组是这样的:

In my opinion, this method is clearer and simpler to manage with multiple options. For example, you may create the array of valid arguments this way:

set i=0
for %%a in (validArgument1 validArgument2 validArgument3) do (
   set /A i+=1
   set arg[!i!]=%%a
)


另一种可能性是定义每个有效参数的变量:


Another possibility is to define a variable for each valid argument:

for %%a in (validArgument1 validArgument2 validArgument3) do set %%a=1

...然后只是检查参数是这样的:

... and then just check the parameter this way:

if defined %1 (
   echo %1 is valid option...
   SET ARG=%1
)

这篇关于如何找到一个字符串是在字符串中的一个DOS批处理文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆