批处理脚本->找到一个句子并用一个新句子代替 [英] Batch script -> find a sentence and replace it by a new one

查看:59
本文介绍了批处理脚本->找到一个句子并用一个新句子代替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究DOS脚本,以便搜索一行并将其替换为特定文件夹中的文件.这是我已经完成的事情:

I'm working on a DOS script in order to search a line and replace it into files from a specific folder. Here is what i have already done :

@echo off 
setlocal enableextensions disabledelayedexpansion

set "search=@Interceptors({ RuntimeExceptionInterceptor.class }^)"
set "replace=@Interceptors({ RuntimeExceptionInterceptor.class, ReportInterceptor.class, CorrelationIdInterceptor }^)"

set "textFile=C:\Utilisateurs\a669884\Documents\test.txt"

for %%a in (*.txt) do (
echo "%%a"
for /f "Delims=" %%i  in (%%a ^& break ^> %%a) do (
    set "line=%%i "
    setlocal enabledelayedexpansion
    >>%%a echo(!line:%search%=%replace%!
    endlocal
      )
 )

问题是我的第一行没有被新的替换,新的行像下面这样添加:

Problem is that my first line is not replace by the new one, the new line is added under it like that :

@Interceptors({RuntimeExceptionInterceptor.class})

@Interceptors({ RuntimeExceptionInterceptor.class })

@Interceptors({RuntimeExceptionInterceptor.class,ReportInterceptor.class,CorrelationIdInterceptor})

@Interceptors({ RuntimeExceptionInterceptor.class, ReportInterceptor.class, CorrelationIdInterceptor })

您知道为什么我的第一行没有被替换吗?非常感谢塞巴

Do you know why my first line isn't replaced? Thanks a lot Seb

推荐答案

不确定文本文件的设置与例程有关-未使用.

Not sure what setting textfile has to do with the routine - it's not used.

您的原始代码产生了一个投诉,内容是找不到& ,并为我创建了一个空文件.

Your original code produced a complaint that & was not found and created an empty file for me.

for 更改为

for /f "Delims=" %%i  in ('type "%%a" ^& break ^> "%%a"') do (

在您提供的源文件的一行(部分?)加上几条空行的情况下,似乎可以进行替换.

seemed to make the substitution, given the one line (partial?) of sourcefile you've provided, plus a few dummy lines.

使用引号括起来的文件名执行 type ... ,以确保使用分隔符正确运行,替换文件;按指定替换.

execute the type... with the filenames enclosed in quotes to ensure correct operation with separators, replacing the file; substitute as specified.

这篇关于批处理脚本->找到一个句子并用一个新句子代替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆