查找并替换不在批处理脚本中工作

时间:2013-05-15 04:57:43

标签: batch-file

我正在尝试使用查找和替换选项创建批处理脚本,但批处理脚本不执行任何操作,只是显示闪烁的光标并卡在那里。

这是我正在使用的编码:

setlocal
set this=abhinav2

for /f "delims=" %%i in ('^<test\abc.new findstr /n "^"') do ('
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"
    if not "!line!"=="!line:<xyz>mnishamk<abc>" set "line=<xyz>%this%<abc>"
    (echo(!line!)>>test\new.abc
    endlocal
)
)

我不能简单地将mnishamk替换为abhinav2,因为文件中存在许多名称mnishamk

2 个答案:

答案 0 :(得分:0)

您遇到语法问题,请参阅set /?获取帮助并尝试以下操作:

@echo off&setlocal enabledelayedexpansion
set "line=<xyz>mnishamk<abc>"
set "this=abhinav2"

if not "!line!"=="!line:<xyz>mnishamk<abc>=!" set "line=<xyz>%this%<abc>"
echo !line!

输出是:

<xyz>abhinav2<abc>

这也有效:

set "line=!line:<xyz>mnishamk<abc>=<xyz>%this%<abc>!"

答案 1 :(得分:0)

@ECHO off
setlocal
DEL \test\new.abc /F /Q
set this=abhinav2

for /f "delims=" %%i in ('^<\test\abc.new findstr /n "^"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:*:=!"
    if not "!line!"=="!line:<xyz>mnishamk<abc>=!" set "line=!line:<xyz>mnishamk<abc>=<xyz>%this%<abc>!"
    echo(!line!>>\test\new.abc
    endlocal
)

FC \test\abc.new \test\new.abc

这对我有用......