2013-10-24 214 views
0

我有一个字符串像'ntuser.dat ntuser.dat.log ntuser.ini test.bat test1.bat advice.20131024.98767 textdoc.txt'
我想删除所有字符指定的字符串,直到'advice.20131024'
我怎样才能做到这一点使用Windows批处理命令?
我还需要保存结果串中的可变
由于事先删除字符,直到一个特定的子字符串

+1

您是否想要在'advice.20131024'之后删除位? – foxidrive

+0

是的,建议前的所有人物.20131024。 advice.20131024可以位于给定字符串中的任意位置。 – user2907999

+0

但你想保持正确的东西? – Monacraft

回答

1

的(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)与用于循环

@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% 

(三)看到foxidrive答案(我总是忘记)

+0

“建议”后的'.20131024'部分也可以保存在支票中吗?因为给定字符串中可能有多个以'advice'开头的子字符串 – user2907999

+0

如果知道该值,请调整比较字符串和子字符串的长度。在这两个示例中,您都需要将':〜0,6%'改为':〜0,15%' –

+0

'advice'后面的部分。实际上是当前日期。所以我得到它并将它存储在另一个变量中。如何将该变量添加到比较中? – user2907999

4

这设置字符串,
将其更改为删除所有内容直到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%" 
相关问题