如何更换Windows中的批处理文件子 [英] How to replace substrings in windows batch file

查看:107
本文介绍了如何更换Windows中的批处理文件子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我使用批处理文件在Windows ...如何从文件中读取和含=浴Abath Bbath XYZbathABC添加字符串'你好',这样,输出类似文件替换字符串='洗澡'你好Ahello Bhello XYZhelloABC

Can anyone tell me using batch file in windows ...how to read from a file and replace string='bath' from file containing="bath Abath Bbath XYZbathABC" with string 'hello' so that the output is like "hello Ahello Bhello XYZhelloABC"

推荐答案

这舍甫琴科中号扩大,是的,你可以从文件做到这一点,即使是多行

Expanding from Andriy M, and yes you can do this from a file, even one with multiple lines

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=test.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=bath
set REPLACETEXT=hello
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

修改

感谢大卫·尼尔森,我已经更新了脚本,以便它不具有硬codeD值了。

Thanks David Nelson, I have updated the script so it doesn't have the hardcoded values anymore.

这篇关于如何更换Windows中的批处理文件子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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