用带有wildchar的cmd替换字符串

时间:2013-05-04 13:30:21

标签: cmd substitution

我有一个包含一些行的文本文件

line1
line2
helloabcd
line4

我想创建一个批处理文件,用%this%来编辑helloabcd行,但我不知道abcd我所知道的是hello。那我怎么办呢?

我尝试使用fart.exe,但是当有通配符时它永远不会替换

fart.exe txt1.txt hello* %this%

可以使用纯cmd,甚至是放屁吗?

提前感谢您的回复

编辑:确定已尝试fart.exe txt1.txt hello %this%,但现在我获得了%this%abcd而不是%this%

1 个答案:

答案 0 :(得分:1)

试试这个:

@echo off &setlocal
set "this=wqreqwrq"
for /f "delims=" %%i in ('^<txt1.txt findstr /n "^"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"
    if not "!line!"=="!line:hello=!" set "line=%this%"
    (echo(!line!)>>output.txt
    endlocal
)

输出位于output.txt

编辑:添加。 “!”除去。