Solved Batch script to find and replace a string in text file

April 24, 2019 at 02:55:27
Specs: Windows 7
I have a multiple text file that contain a huge data and want to replace some data.

contain text is :

[LDDAT:0000000," ","01","B16",1,72,72,975,"99L40",0,975,0,0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","03","B12",1,34,34,1380,"99L45",200,450,(775),0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","04","B12",1,156,156,1155,"99190",225,200,(775),0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","06","B12",1,34,34,1230,"99190",200,300,(775),0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]

I want to change:-

Search "99L40" is a reference then find ,,,0,0,0,,,0,0,0 --> replace with ,text1,,0,0,0,,,0,0,0
Search "99L45" is a reference then find ,,,0,0,0,,,0,0,0 --> replace with ,text2,,0,0,0,,,0,0,0

We need result :-

[LDDAT:0000000," ","01","B16",1,72,72,975,"99L40",0,975,0,0,0,0,0,0,0,0,0,text1,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","03","B12",1,34,34,1380,"99L45",200,450,(775),0,0,0,0,0,0,0,0,text2,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","04","B12",1,156,156,1155,"99190",225,200,(775),0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","06","B12",1,34,34,1230,"99190",200,300,(775),0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]

Can anyone help me with the batch file?

Thanks
Pugazh


See More: Batch script to find and replace a string in text file

Report •

✔ Best Answer
April 25, 2019 at 01:46:19
nvm, Done.

This will run the same script for all .txt in the directory, where test.bat is executed.

test.bat:

@echo off&setlocal enabledelayedexpansion
:DirLoop
for /f %%a in ('dir/b/A-D ^| find ".txt"') do (
	call :Presets "%%a"
)
echo DONE^^!
echo.
echo exiting in 5 seconds...
timeout 5 >nul
exit/b
:presets
if "%1"=="" echo No inputfile detected^^!&pause>nul&exit
set "inputfile=%~nx1"
set "outputfile=%~n1.bak"
set "text1=place text1 here"
set "text2=place text2 here"
:Main
@echo off>%outputfile%
for /f "tokens=1-21,* delims=," %%a in (!inputfile!) do (
	set "candidateU=%%u"
	if "%%i"==""99L40"" set "candidateU=!text1!"
	if "%%i"==""99L45"" set "candidateU=!text2!"
	if "%%h"=="" (
		echo %%a>>%outputfile%
	) else (
		echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,!candidateU!,%%v>>%outputfile%
	)
)
del "%~1"
ren "%outputfile%" "%inputfile%"
exit/b

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded



#1
April 24, 2019 at 02:58:44
working on it^^!

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#2
April 24, 2019 at 03:02:27
Really :) I'm waiting here..!!

Report •

#3
April 24, 2019 at 03:46:00
these values in test.bat can be changed:

set "inputfile=input.txt"
set "outputfile=output.txt"
set "text1=place text1 here"
set "text2=place text2 here"

input.txt:

[LDDAT:0000000," ","01","B16",1,72,72,975,"99L40",0,975,0,0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","03","B12",1,34,34,1380,"99L45",200,450,(775),0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","04","B12",1,156,156,1155,"99190",225,200,(775),0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]
[LDDAT:0000000," ","06","B12",1,34,34,1230,"99190",200,300,(775),0,0,0,0,0,0,0,0,,,0,0,0,,,0,0,0]

output.txt:

[LDDAT:0000000," ","01","B16",1,72,72,975,"99L40",0,975,0,0,0,0,0,0,0,0,0,place text1 here,0,0,,,0,0,0]
[LDDAT:0000000," ","03","B12",1,34,34,1380,"99L45",200,450,(775),0,0,0,0,0,0,0,0,place text2 here,0,0,,,0,0,0]
[LDDAT:0000000," ","04","B12",1,156,156,1155,"99190",225,200,(775),0,0,0,0,0,0,0,0,0,0,0,,,0,0,0]
[LDDAT:0000000," ","06","B12",1,34,34,1230,"99190",200,300,(775),0,0,0,0,0,0,0,0,0,0,0,,,0,0,0]

test.bat:

@echo off&setlocal enabledelayedexpansion
:presets
set "inputfile=input.txt"
set "outputfile=output.txt"
set "text1=place text1 here"
set "text2=place text2 here"
:Main
@echo off>%outputfile%
for /f "tokens=1-21,* delims=," %%a in (!inputfile!) do (
	set "candidateU=%%u"
	if "%%i"==""99L40"" set "candidateU=!text1!"
	if "%%i"==""99L45"" set "candidateU=!text2!"
	echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,!candidateU!,%%v>>%outputfile%
)

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded

message edited by hidde663


Report •

Related Solutions

#4
April 24, 2019 at 03:55:06
hold on, looks like i miscounted by 2 :D

edit: nope, batch just cant handle double comma's

could you ctrl+h (replace) all ,,, with , , , and ,, with , ,
that would fix the issue

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded

message edited by hidde663


Report •

#5
April 24, 2019 at 04:05:40
Hi @hidde663

Sorry, i forgot to tell you one step, i want to skip some line

See here, i got this Output

[MHMEM:"B1"],,,,,,,,,,,,,,,,,,,,,
[LDDAT:0000000," ","01","B16",1,72,72,975,"99L40",0,975,0,0,0,0,0,0,0,0,0,place text1 here,0,0,,,0,0,0]
[LDDAT:0000000," ","03","B12",1,34,34,1380,"99L45",200,450,(775),0,0,0,0,0,0,0,0,place text2 here,0,0,,,0,0,0]
[LDDAT:0000000," ","04","B12",1,156,156,1155,"99190",225,200,(775),0,0,0,0,0,0,0,0,0,0,0,,,0,0,0]
[LDDAT:0000000," ","06","B12",1,34,34,1230,"99190",200,300,(775),0,0,0,0,0,0,0,0,0,0,0,,,0,0,0]

I need to skip this line :-

[MHMEM:"B1"],,,,,,,,,,,,,,,,,,,,,

Search "MHMEM" is a reference then skip this line is it possible?

I Need this Output result :-

[MHMEM:"B1"]
[LDDAT:0000000," ","01","B16",1,72,72,975,"99L40",0,975,0,0,0,0,0,0,0,0,0,place text1 here,0,0,,,0,0,0]
[LDDAT:0000000," ","03","B12",1,34,34,1380,"99L45",200,450,(775),0,0,0,0,0,0,0,0,place text2 here,0,0,,,0,0,0]
[LDDAT:0000000," ","04","B12",1,156,156,1155,"99190",225,200,(775),0,0,0,0,0,0,0,0,0,0,0,,,0,0,0]
[LDDAT:0000000," ","06","B12",1,34,34,1230,"99190",200,300,(775),0,0,0,0,0,0,0,0,0,0,0,,,0,0,0]

and i have multiple text file with different file name any options for search (*.txt) and i need output file is same original file name no need .bak

Thanks
Pugazh

message edited by Pugazh


Report •

#6
April 24, 2019 at 04:52:07
it's drag and drop now, also from line 1 all comma's are removed, like so:
[MHMEM:"B1"]

backups are currently not made, so if you're planning to try it out, backup the files manually please.

did you take a look at my answer #4, because it's quite important?


current setup:


input.txt before test.bat is run:

[MHMEM:"B1"],,,,,,,,,,,,,,,,,,,,,
[LDDAT:0000000," ","01","B16",1,72,72,975,"99L40",0,975,0,0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","03","B12",1,34,34,1380,"99L45",200,450,(775),0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","04","B12",1,156,156,1155,"99190",225,200,(775),0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","06","B12",1,34,34,1230,"99190",200,300,(775),0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]

input.txt after test.bat is run:

[MHMEM:"B1"]
[LDDAT:0000000," ","01","B16",1,72,72,975,"99L40",0,975,0,0,0,0,0,0,0,0,0,place text1 here, ,0,0, , ,0,0,0]
[LDDAT:0000000," ","03","B12",1,34,34,1380,"99L45",200,450,(775),0,0,0,0,0,0,0,0,place text2 here, ,0,0, , ,0,0,0]
[LDDAT:0000000," ","04","B12",1,156,156,1155,"99190",225,200,(775),0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","06","B12",1,34,34,1230,"99190",200,300,(775),0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]


test.bat:

@echo off&setlocal enabledelayedexpansion
:presets
if "%1"=="" echo No inputfile detected^^!&pause>nul&exit/b
set "inputfile=%~nx1"
set "inputfilepath=%~dp1"
set "outputfile=%~dp1%~n1.bak"
set "text1=place text1 here"
set "text2=place text2 here"
:Main
@echo off>%outputfile%
for /f "tokens=1-21,* delims=," %%a in (!inputfile!) do (
	set "candidateU=%%u"
	if "%%i"==""99L40"" set "candidateU=!text1!"
	if "%%i"==""99L45"" set "candidateU=!text2!"
	if "%%h"=="" (
		echo %%a>>%outputfile%
	) else (
		echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,!candidateU!,%%v>>%outputfile%
	)
)
del "%~1"
ren "%outputfile%" "%inputfile%"

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded

message edited by hidde663


Report •

#7
April 24, 2019 at 05:25:06
Hi @hidde663

Yeah i can did it by your answer #4.

No inputfile detected showing here!
How to take input file please tell me .because, i don't know batch script fully.

And not only this [MHMEM:"B1"] line skip

I need to change only if "%%i"==""99L40"" & if "%%i"==""99L45"" skip don't change anything do other lines.

Thanks
Pugazh

message edited by Pugazh


Report •

#8
April 24, 2019 at 05:38:28
like i said, it's drag&drop now.

Simply drag the inputfile ontop of test.bat, like this:
https://i.stack.imgur.com/qaYpg.gif

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#9
April 24, 2019 at 05:59:28
Oh yeah what a magic :)

Thank you for your response. It's worked perfectly nice script

Thank you :) @hidde663 :)

Thanks
Pugazh


Report •

#10
April 24, 2019 at 23:37:50
Hi @hidde663

I have some doubt. Sorry @hidde663

drag & drop method is very difficult. if any other options for select multiple text file? like this ('dir /B *.txt') it's possible?

Thanks
Pugazh

message edited by Pugazh


Report •

#11
April 25, 2019 at 01:33:05
sure, check back in about 3 hours as i do not have a lot of time right now.

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#12
April 25, 2019 at 01:44:27
Hi @hidde663

Take your valuable time. Thank you :) @hidde663

message edited by Pugazh


Report •

#13
April 25, 2019 at 01:46:19
✔ Best Answer
nvm, Done.

This will run the same script for all .txt in the directory, where test.bat is executed.

test.bat:

@echo off&setlocal enabledelayedexpansion
:DirLoop
for /f %%a in ('dir/b/A-D ^| find ".txt"') do (
	call :Presets "%%a"
)
echo DONE^^!
echo.
echo exiting in 5 seconds...
timeout 5 >nul
exit/b
:presets
if "%1"=="" echo No inputfile detected^^!&pause>nul&exit
set "inputfile=%~nx1"
set "outputfile=%~n1.bak"
set "text1=place text1 here"
set "text2=place text2 here"
:Main
@echo off>%outputfile%
for /f "tokens=1-21,* delims=," %%a in (!inputfile!) do (
	set "candidateU=%%u"
	if "%%i"==""99L40"" set "candidateU=!text1!"
	if "%%i"==""99L45"" set "candidateU=!text2!"
	if "%%h"=="" (
		echo %%a>>%outputfile%
	) else (
		echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,!candidateU!,%%v>>%outputfile%
	)
)
del "%~1"
ren "%outputfile%" "%inputfile%"
exit/b

i5-6600K[delid]@4.9GHz/4.4GHz cache/@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded


Report •

#14
April 25, 2019 at 01:55:35
Cheers @hidde663

Perfect!! Thank you for give me your valuable time.

Thank you :) @hidde663 :)

Thanks
Pugazh


Report •

#15
April 25, 2019 at 18:09:35
I'm glad this was satisfactorily answered, but I feel compelled to mention that a simple text string can be searched for in a file, with a search and replace function, using Powershell.

The Powershell script can be run from a batch file, and the script is only three lines of code (which can be expanded to do as many replace operations as you wish, at a 'cost' of 3 extra lines of code for each extra one. But at its most basic is just 3 lines.

Basically you can echo the following into a .ps1 file from a batch file, then use the batch file to launch the .ps1 file too. The .ps1 file looks like this -

$file = "file name.txt"

$find = "ASCII text to search for"
$replace = "Replacement text"

Get-Content "$file" | ForEach-Object { $_ -replace $find,$replace } | Set-Content "$file 001";


Report •

#16
April 26, 2019 at 08:47:15
I know and thanks, but i know way too much batch and about 0 in powershell.
Will be learning powershell somewhere this year :D

also RE #15 check this out ed999:
https://www.computing.net/answers/p...

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2250MHz@1500MHz timings BiosModded

message edited by hidde663


Report •

Ask Question