-
Notifications
You must be signed in to change notification settings - Fork 9
/
ReplaceInFile.bat
57 lines (35 loc) · 972 Bytes
/
ReplaceInFile.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@echo off
REM Replaces a line in a file,
REM indicated by the first word of each line.
REM This method takes three arguments:
REM the file
REM the word or indicator
REM the new line contents
setlocal enabledelayedexpansion
set srcfile=filesrc.txt
set infile=file.txt
rem Copy the source file so i don't have to worry about losing it..
copy /y "%srcfile%" "%infile%"
set "LineId=%~1"
set "NewText=%~2"
call :ReplaceInFile %infile% %LineId% "%NewText%"
goto :EOF
:ReplaceInFile
set tmpfile="%~1.tmp"
(
for /f "usebackq tokens=*" %%G in (%~1) do (
rem %%G will have each line of file
for /F "tokens=1" %%H in ("%%G") do (
rem %%H will have the first word of %%G
if /i "%%H" NEQ "%~2" (
echo:%%G
) else (
echo:%%H %~3
)
)
)
) > "%tmpfile%"
if not %errorlevel% == 0 goto :EOF
del /f /q "%1"
rename "%tmpfile%" "%1"
goto :EOF