Batch file 删除字符直到指定子字符串

Batch file 删除字符直到指定子字符串,batch-file,Batch File,我有一个类似“ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt”的字符串 我想删除指定字符串中的所有字符,直到'advice.20131024' 如何使用windows批处理命令执行此操作? 我还需要将结果字符串保存在变量中 提前感谢(a)在字符串中搜索 set text=ntuser.dat ntuser.dat.log ntuser.ini test.ba

我有一个类似“ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt”的字符串
我想删除指定字符串中的所有字符,直到'advice.20131024'
如何使用windows批处理命令执行此操作?
我还需要将结果字符串保存在变量中
提前感谢

(a)在字符串中搜索

    set text=ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt

:loop
    if "%text:~0,6%"=="advice" goto exitLoop
    set text=%text:~1%
    goto loop

:exitLoop
    echo %text%
(b) 带for循环

@echo off
    setlocal enableextensions enabledelayedexpansion

    set text=ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt
    set result=

    for %%f in (%text%) do (
        set x=%%f
        if "!x:~0,6!"=="advice" (
            set result=%%f
        ) else (
            if not "!result!"=="" set result=!result! %%f
        )
    )

    echo %result%
(c) 查看foxidrive答案(我总是忘记这一点)

(a)在字符串中搜索

    set text=ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt

:loop
    if "%text:~0,6%"=="advice" goto exitLoop
    set text=%text:~1%
    goto loop

:exitLoop
    echo %text%
(b) 带for循环

@echo off
    setlocal enableextensions enabledelayedexpansion

    set text=ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt
    set result=

    for %%f in (%text%) do (
        set x=%%f
        if "!x:~0,6!"=="advice" (
            set result=%%f
        ) else (
            if not "!result!"=="" set result=!result! %%f
        )
    )

    echo %result%
(c) 参见foxidrive答案(我总是忘记这一点)

这设置了字符串,
将其更改为删除所有内容,直到
advice
结束,并将其替换为
advice

然后回音字符串的其余部分

set "string=ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt"
set "string=%string:*advice=advice%"
echo "%string%"
这将设置字符串,
将其更改为删除所有内容,直到
advice
结束,并将其替换为
advice

然后回音字符串的其余部分

set "string=ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt"
set "string=%string:*advice=advice%"
echo "%string%"

是否要删除advice.20131024之后的位?是的,advice.20131024之前的所有字符。advice.20131024可以位于给定字符串中的任意位置。但您想在之后保持内容正确吗?是的,需要在之后的内容。是否要删除该
advice.20131024
之后的位?是的,advice.20131024之前的所有字符。advice.20131024可以位于给定字符串中的任意位置。但是您想在以后保留正确的内容吗?是的,之后需要的内容“advice”后面的“.20131024”部分也可以保留在支票中吗?由于给定字符串中可能有多个子字符串以“advice”开头,如果您知道该值,请调整比较字符串和子字符串的长度。在这两个示例中,您都需要将
:~0,6%
更改为
:~0,15%
,“advice.”后面的部分实际上是当前日期。所以我得到它并将其存储在另一个变量中。如何将该变量添加到比较中?您到底需要什么?第一个通知和日期,所有通知和日期,特定日期的所有通知,…是否可以在支票中保留“通知”后的“.20131024”部分?由于给定字符串中可能有多个子字符串以“advice”开头,如果您知道该值,请调整比较字符串和子字符串的长度。在这两个示例中,您都需要将
:~0,6%
更改为
:~0,15%
,“advice.”后面的部分实际上是当前日期。所以我得到它并将其存储在另一个变量中。如何将该变量添加到比较中?您到底需要什么?第一条通知和日期,所有通知和日期,特定日期的所有通知。。。