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

 

你可能感兴趣的:(BAT:文件中替换字符(保留空格和换行))