BAT:文件中替换字符(保留空格和换行)

 1 @echo off
 2 rem CMD:"color --help"
 3 :: setting color:back white,content blue
 4 color f9
 5 
 6 ::old string and replace string
 7 set old_str=ip
 8 set new_str=#ip
 9 
10 :FILE
11 set "souerce_path=D:\xxx\bat\my_test.txt"
12 for /f "tokens=1* delims=:" %%i in ('findstr /n .* "%souerce_path%"') do (
13     set "lineContent=%%j"
14     ::echo ==%%j==
15     setlocal EnableDelayedExpansion
16     if not "!lineContent!"=="" set "lineContent=!lineContent:%old_str%=%new_str%!"
17     >>"temp.txt" echo.!lineContent!
18     endlocal
19 )
20 move /y temp.txt "%souerce_path%"
21 goto END
22 
23 :PATH
24 ::files path
25 set "souerce_path=D:/xxx/bat"
26 cd /d "%souerce_path%"
27 
28 :: dir /s /b /a-d *.txt  ::get all current and sub path file name with post .txt
29 for /f "delims="  %%a in ('dir /s /b /a-d *.txt') do ( 
30     for /f "tokens=1* delims=:" %%i in ('findstr /n .* "%%a"') do (
31         set "lineContent=%%j"
32         ::echo ==%%j==
33         ::here use "setlocal EnableDelayedExpansion" to keep content "!"
34         setlocal EnableDelayedExpansion
35         if not "!lineContent!"=="" set "lineContent=!lineContent:%old_str%=%new_str%!"
36         >>"%%~dpnxas" echo.!lineContent!
37         endlocal
38     )
39     del /s /q /f "%%a" >nul 
40     ren *.txts *.txt
41 )
42 :END
43 echo   ---------ok----------
44 pause>nul

 

转载于:https://www.cnblogs.com/leonlipfsj/p/10164005.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值