-
Notifications
You must be signed in to change notification settings - Fork 0
/
search and replace.bat
40 lines (33 loc) · 1.14 KB
/
search and replace.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@echo off
setlocal enabledelayedexpansion
:begin
rem Prompt user for file extensions to include in search
set /p "search_ext=Enter file extension: "
rem Prompt user for text to search for
set /p "search_text=Enter text to search for: "
rem Prompt user for replacement text
set /p "replace_text=Enter replacement text: "
rem Loop through all search_ext files in the directory
for /R %%f in (*.%search_ext%) do (
rem Perform search and replace operation on each file
set "output_file=%%~nf.SR.%search_ext%"
(
for /f "usebackq delims=" %%l in ("%%f") do (
set "line=%%l"
rem Skip empty lines
set "is_empty="
if "!line!"=="" set "is_empty=true"
if "!is_empty!"=="true" (
echo.
) else (
setlocal enabledelayedexpansion
set "line=!line:%search_text%=%replace_text%!"
echo(!line!
endlocal
)
)
) > "!output_file!"
rem Replace the original file with the new file
move /y "!output_file!" "%%f"
)
GOTO begin