如何使用批处理文件在循环中的字符串中找到一个子字符串?

发布于 2025-02-01 10:02:00 字数 699 浏览 4 评论 0原文

这是我使用的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2025-02-08 10:02:00
@ECHO OFF
setlocal enabledelayedexpansion
set argCount=0
set "str1=hello"
SET /a cond=0
for %%x in (%*) do (
 set /A argCount+=1
 set "argVec[!argCount!]=%%~x"
 IF NOT "%str1%"=="!str1:%%~x=!" SET /a cond=argcount
 ECHO !cond!
)
echo Number of processed arguments: %argCount%
GOTO :EOF

将显示以condcond将其发现的参数编号将其设置为最后一个查找或0,如果没有找到,则将设置为0

@ECHO OFF
setlocal enabledelayedexpansion
set argCount=0
set "str1=hello"
SET /a cond=0
for %%x in (%*) do (
 set /A argCount+=1
 set "argVec[!argCount!]=%%~x"
 IF NOT "%str1%"=="!str1:%%~x=!" SET /a cond=argcount
 ECHO !cond!
)
echo Number of processed arguments: %argCount%
GOTO :EOF

would show the argument number in which the substring was found as cond and cond would be set to the last of these finds or 0 if none was found

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文