删除批处理(.bat)文件中的部分字符串

时间:2014-03-26 16:48:47

标签: batch-file batch-rename

我需要删除部分字符串,以便我可以使用该消息。尝试了许多不同的方法失败了。提前致谢

想象我有这样一条消息:

SET MESSAGE="The key sequence '1' in 'http://www.myschema.com/dummy:dummy_PK' Keyref fails to refer to some key."

我需要删除它:

in 'http://www.myschema.com/dummy:dummy_PK' Keyref

得到这样的东西:

"The key sequence '1' fails to refer to some key."

请注意" in"之间的文字。和" Keyref"是变量,这意味着我必须找到一种方法来删除这两个字符串之间的文本(包括)。

2 个答案:

答案 0 :(得分:0)

试试这个:

@echo off
setlocal

SET "MESSAGE=The key sequence '1' in 'http://www.myschema.com/dummy:dummy_PK' Keyref fails to refer to some key."
SET "sFind=in 'http://www.myschema.com/dummy:dummy_PK' Keyref "
call set New=%%Message:%sFind%=%%
echo %New%

答案 1 :(得分:0)

假设inKeyref只出现一次,|永不出现,那么:

@echo off
setlocal enableDelayedExpansion
set message="The key sequence '1' in 'http://www.myschema.com/dummy:dummy_PK' Keyref fails to refer to some key."
set "message=!message: in =|!"
set "message=!message: Keyref =|!"
for /f "delims=| tokens=1,3" %%A in ("!message!") do set "message=%%A %%B"
set message