检查bat文件脚本,如果字符串包含另一个字符串 [英] bat file script to check if string contains other string

查看:3123
本文介绍了检查bat文件脚本,如果字符串包含另一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个批处理文件,将检查是否变量包含特定值。
我试着做到以下几点:

I need to write a batch file that will check if a variable contains specific value. I tried to do the following:

If "%%a"=="%%a:%pattern%" (
    echo Yes
) else (
   echo No
)

输入例如:
%%一个=鲍勃binson
%百通(%)=binson

input example: %%a="bob binson" %patern%="binson"

我永远不会是印!
谁能告诉我错过或放弃的他怎么会(S)办呢?一个例子

I never get Yes printed! can anyone please tell what i missed or give an example of how (s)he would do it?

由于提前

推荐答案

子字符串操作是不是在替换参数。您需要将数据分配给一个变​​量,然后在这个变量执行操作

Substring operations are not available in for replaceable parameters. You need to assign the data to a variable and then execute the operation on this variable

@echo off
    setlocal enableextensions disabledelayedexpansion

    >"tempFile" (
        echo bob binson
        echo ted jones
        echo binson
    )

    set "pattern=binson"

    for /f "usebackq delims=" %%a in ("tempFile") do (
        echo data: %%a

        set "line=%%a"
        setlocal enabledelayedexpansion
        if "!line:%pattern%=!"=="!line!" (
            echo .... pattern not found
        ) else (
            echo .... pattern found
        )
        endlocal
    )

    del /q tempFile

这篇关于检查bat文件脚本,如果字符串包含另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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