gpt4 book ai didi

windows - 检查子字符串是否包含字符串

转载 作者:可可西里 更新时间:2023-11-01 11:19:58 24 4
gpt4 key购买 nike

我正在尝试检查子字符串是否出现在字符串中。如果是,那么我不想执行“if”条件。

我的问题:我检查字符串中是否出现子字符串的代码无法正常工作。它总是认为子字符串实际上没有出现在字符串中。

如何批量判断字符串中是否出现子字符串?

SET filePath="c:/users/abc/dir1/subdir"
SET excludeDir1="c:/users/abc/dir1"
SET excludeDir2="c:/users/abc/dir2"

REM // If the string excludeDir1 does not occur in filePath AND If the string excludeDir2 does not occur in filePath: continue
if /i NOT x%filePath:%excludeDir1%=%==x%filePath% if /i NOT x%filePath:%excludeDir2%=%==x%filePath% (
REM // Do stuff
)

最佳答案

你几乎拥有它。请记住,批处理文件中的行的解析是从左到右执行的,因此没有机会嵌套两个 %variable% 扩展。解决它的方法是结合一个 %normal% 扩展和一个 !delayed!展开:

REM Next command is required in order to use Delayed !variable! Expansion
SETLOCAL EnableDelayedExpansion

SET filePath="c:/users/abc/dir1/subdir"
SET excludeDir1="c:/users/abc/dir1"
SET excludeDir2="c:/users/abc/dir2"

REM // If the string excludeDir1 does not occur in filePath AND If the string excludeDir2 does not occur in filePath: continue
if /i NOT "!filePath:%excludeDir1%=!" == "%filePath%" if /i NOT "!filePath:%excludeDir2%=!" == "%filePath%" (
REM // Do stuff
)

关于windows - 检查子字符串是否包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16703472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com