如何使用批处理文件在循环中的字符串中找到一个子字符串?
这是我使用的代码:
setlocal enabledelayedexpansion
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
)
echo Number of processed arguments: %argCount%
set str1=hello
for /L %%i in (1,1,%argCount%) do (
set "str2=!argVec[%%i]!"
If NOT "!str1!"=="!str1:!str2!=!" set COND=1
echo !COND!
)
此代码计数传递给批处理文件的参数并将其分配给环境变量。仅当参数字符串包含字符串Hello
时,环境变量cond
才能使用1
定义。
问题在此行中:
If NOT "!str1!"=="!str1:!str2!=!" set COND=1
变量cond
始终用1
定义。
有什么想法我该如何解决?
PS:str2
使用当前参数(参数)定义,以检查Hello
。
This is the code used by me:
setlocal enabledelayedexpansion
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
)
echo Number of processed arguments: %argCount%
set str1=hello
for /L %%i in (1,1,%argCount%) do (
set "str2=!argVec[%%i]!"
If NOT "!str1!"=="!str1:!str2!=!" set COND=1
echo !COND!
)
This code counts the parameters passed to the batch file and assign them to environment variables. The environment variable COND
should be defined with 1
only if a parameter string contains the string hello
.
The problem is in this line:
If NOT "!str1!"=="!str1:!str2!=!" set COND=1
Variable COND
is always defined with 1
.
Any ideas how can I solve it?
PS: str2
is defined with the current parameter (argument) to check for hello
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将显示以
cond
和cond
将其发现的参数编号将其设置为最后一个查找或0,如果没有找到,则将设置为0would show the argument number in which the substring was found as
cond
andcond
would be set to the last of these finds or 0 if none was found