bat文件修改或替换文件内容
rem 定义变量延迟环境,关闭回显
@echo off&setlocal enabledelayedexpansion
rem 读取a.txt所有内容
for /f "eol=* tokens=*" %%i in (a.txt) do (
rem 设置变量a为每行内容
set a=%%i
rem 如果该行有123,则将其改为456
set "a=!a:123=456!"
rem 把修改后的全部行存入$
echo !a!>>$)
rem 用$的内容替换原来a.txt内容
move $ a.txt
出处:https://blog.csdn.net/everdayPHP/article/details/78919020
===============================================================
查找固定的内容并替换的为固定文本的情况:
文件A.txt是需要替换的文件,运行下面这个批处理:
@echo off&setlocal EnableDelayedExpansion for /f "delims=" %%b in ('type A.txt') do ( set "str=%%b"&set "str=!baistr:ABC=123!"&set "str=!str:XYZ=AFK!" echo !str! >>new_A.txt ) start new_A.txt
下面这个是不去空行的,有必要你就用吧,有点长!你复制过去删除多余空格,不行留邮箱发给你。百度老是存在这个问题,贴出来再复制回去格式就不一样了。真气人!!!
@echo off&setlocal EnableDelayedExpansion for /f "delims=" %%a in ('findstr /n .* A.txt') do ( set "str=%%a" set "str=!str:ABC=123!" set "str=!str:XYZ=AFK!" echo !str:~1! >>tmp.txt ) for /f "delims=" %%b in ('type tmp.txt') do ( set "str=%%b" if "!str!"==": " ( call echo. >>new_A.txt ) else ( echo !str:~1! >>new_A.txt )) del tmp.txt&start new_A.txt
出处:https://zhidao.baidu.com/question/84256975.html
=============================================================
根据上面的参考,自己稍微修改点,可以适用于自己的需求的程序
自己联系和测试程序
rem 定义变量延迟环境,关闭回显 @echo off setlocal enabledelayedexpansion ::goto :aa ::goto :bb2 goto :cc rem 读取a.txt所有内容 for /f "eol=* tokens=*" %%i in ('type test.txt') do ( rem 设置变量a为每行内容 set a=%%i rem 如果该行有123,则将其改为456 echo !a!-- set "a=!a:dd*=456!" rem 把修改后的全部行存入$ echo !a!>>$) rem 用$的内容替换原来a.txt内容 move $ test.txt goto :eof :aa for %%i in (test.txt) do set file=%%~fi echo ========== for /f "delims=" %%i in ('type test.txt') do ( set str=%%i set "str=!str:dd=456!" echo !str!>>$ ) move $ test.txt goto :eof :bb1 set old= for /f "delims=" %%a in ('findstr .* test.txt') do ( set "str=%%a" set "str=!str:ABC=123!" set "str=!str:XYZ=AFK!" echo !str! ) goto :eof :bb2 set old= rem 查找并匹配最后一行,包含king的字符串 for /f "eol=*" %%a in ('findstr /b "king" test.txt') do (set "old=%%a") echo %old%=============== rem 再次查找完全陪陪的行,并替换queen的字符串 for /f "delims=" %%b in ('type test.txt') do ( set "str=%%b" if "!str!"=="%old%" (set str=queen[00-11-22]) echo !str!>>$ ) move $ test.txt goto :eof :cc set aa=kjk dfdasf jlksj ij hgn my name AA set bb=my echo.%aa% | findstr /C:"%bb%" goto :eof
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/13803098.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2020-10-12 15:19 jack_Meng 阅读(29315) 评论(0) 编辑 收藏 举报