码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-12-23 15:19:39      阅读:421      评论:0      收藏:0      [点我收藏+]

标签:get   替换   back   hit   test   dexp   and   current   lims   

 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

 

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

标签:get   替换   back   hit   test   dexp   and   current   lims   

原文地址:https://www.cnblogs.com/leonlipfsj/p/10164005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!