我需要删除以Windows批处理脚本中的字符串中的单词开头的所有内容。 例如,如果我有字符串:
This is my line delete from here.
我想删除以delete一词开头的所有内容,我需要获取:
This is my line.
我试过这个:
set line="This is my line delete from here"
set word="delete"
set delete="!word!*";
set line=!line:%delete%=!
它无法正常运行,我已经激活延迟扩展,我不确定是否:“set line =!line:%delete%=!”作品。我也试过这个“set line =!line :! delete!=!”但也不起作用。我是批处理脚本的新手。
更新: 在循环中,我只能进行延迟扩展,并且此代码不起作用:
@ECHO OFF
setlocal
for /F "delims= " %%A in (temp.txt) do (
set "line=This is my line delete from here"
ECHO original:%line%:
set "word=delete"
CALL set "delete=%%line:*%word%=%%"
ECHO delete the "%word%%delete%" part
CALL set "line=%%line:%word%%delete%=%%"
ECHO final :%line%:
)
endlocal
输出:
original::
=%" was unexpected at this time.
delete the "" part
=%" was unexpected at this time.
final ::
但是如果从上面的代码中删除for,则输出为:
original:This is my line delete from here:
delete the "delete from here" part
final :This is my line :
我的问题是如何在循环中做同样的事情,延迟扩展,就像正常扩展一样。换句话说,我需要在替换表达式内部和外部的两个变量的后期扩展(设置“delete =!line:*!word!=!”,不起作用)。我找不到任何相关的文档。 感谢。
答案 0 :(得分:1)
@ECHO OFF
SETLOCAL
set "line=This is my line delete from here"
ECHO original:%line%:
set "word=delete"
CALL set "delete=%%line:*%word%=%%"
ECHO delete the "%word%%delete%" part
CALL set "line=%%line:%word%%delete%=%%"
ECHO final :%line%:
GOTO :EOF
这应该会显示步骤。
首先,删除包含该单词的部分。然后删除单词和剩余行(delete
)
注意引号的位置 - 控制set
而不是分配的值。
答案 1 :(得分:0)
@ECHO OFF
setlocal
for /F "delims= " %%A in (temp.txt) do (
set "line=This is my line delete from here"
set "word=delete"
echo line is: !line!
for /f "delims=" %%a in ("!word!") do set delete=!line:*%%a=!
echo delete is: !delete!
for /f "delims=" %%a in ("!word!!delete!") do set line=!line:%%a=!
echo line is: !line!
)
endlocal