Page 1 of 2
Batch file edit text file
Posted: 28 Jul 2013 11:52
by Beach Boy
Is it possible for a batch file to: search a given text file for a given word/string and change that word/string then save it with the changes? I have search online but not finding this specific script that will help. The file I am looking to work with is a .reg file if that helps. Thank you.
Re: Batch file edit text file
Posted: 28 Jul 2013 12:25
by penpen
In many cases something like this suffices:
Code: Select all
@echo off
set "replace=something"
set "replaced=different"
set "source=Source.txt"
set "target=Target.txt"
setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
)
) > %target%
endlocal
penpen
Edit: Corrected error: missing '' in loop
Edit2: Removed 2 bugs on undefined line.
Re: Batch file edit text file
Posted: 28 Jul 2013 14:27
by Beach Boy
penpen,
I tried this - it seems to just create a blank file with the given "target" name. No changes in the origial file as reqested even given the settings for replaced and replace that don't seem to change.
Re: Batch file edit text file
Posted: 28 Jul 2013 15:45
by penpen
Sorry i didn't test it.
I've corrected the code above, and it should work now.
penpen
Re: Batch file edit text file
Posted: 28 Jul 2013 16:02
by Beach Boy
My output file still seems to be blank. I change the values for the 4 "set" lines to match my environment - am I missings something?
Re: Batch file edit text file
Posted: 28 Jul 2013 16:40
by foxidrive
Try it with the values in the bat file. It works, but blank lines give it a hernia.
Re: Batch file edit text file
Posted: 28 Jul 2013 16:56
by penpen
Removed the bugs with empty lines.
It now should work with blank lines, too.
Note you cannot use the = character in replace variable, all other should work.
Code: Select all
:: Source.txt example
This is something from all.
This is something from all.
This is something from all.
Nothing follows.
This is something from all.
This is something from all.
This is something from all.
This is something from all.
End.
penpen
Re: Batch file edit text file
Posted: 29 Jul 2013 11:47
by Beach Boy
Here is my demima: from your PC, go to regedit and export the following hive as "control.reg" -
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\CriticalDeviceDatabase]
Then with the following code tell me if you can change the the word "SYSTEM" to "offline" (on each line) in the source file with the following code, or what output do you see in the target file:
@echo off
set "replace=system"
set "replaced=offline"
set "source=control.reg"
set "target=control2.reg"
setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
)
) > %target%
endlocal
Re: Batch file edit text file
Posted: 29 Jul 2013 12:14
by penpen
Yes, it is working fine.
The only thing, that might happen is, that the reg file on some systems may be exported using Unicode.
To avoid this, as Unicode is not supported, you should transform it to ANSI/ASCII:
Code: Select all
::Export the key
reg.exe export "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\CriticalDeviceDatabase" temp.reg
:: transform to ascii
cmd /a /c type temp.reg > control.reg
:: delete temp file
del temp.reg
As on my pc the export is done in ANSI i easily forget the Unicode problem, sry for that.
penpen
Re: Batch file edit text file
Posted: 29 Jul 2013 12:25
by Beach Boy
That worked greatly. Thank you for the assistance.
Re: Batch file edit text file
Posted: 01 Oct 2020 09:20
by werejago
Is it possible to convert this into having the user input the replaced word and over write the file?
This script seems to just find and replace a given string but how can I use a given string by user input.
My goal here is to have a user open a batch file which asks them to enter a number and have that number replace another number in a text file.
Re: Batch file edit text file
Posted: 02 Oct 2020 02:05
by aGerman
@werejago
Has this been answered?
viewtopic.php?f=3&t=9795&p=63004#p62995
Steffen
Re: Batch file edit text file
Posted: 05 Oct 2020 05:11
by penpen
werejago wrote: ↑01 Oct 2020 09:20
Is it possible to convert this into having the user input the replaced word and over write the file?
Yes, you could use the "set /p" command to store user input into an environment variable.
You can't overwrite the file while reading it (should locked), but you can write to a temporary file and then replace the original file with a copy of the temporary one by using the commands "delete" and "copy".
penpen
Re: Batch file edit text file
Posted: 01 Feb 2021 21:15
by msswamy
Hey Penpen,
This solution really good and useful, but i have 2 problems while dealing with my file.
1. My actual file having special characters(!), it is removed in final result file.
2. How can i insert new line in replaced string.
Can you please help me here.
Re: Batch file edit text file
Posted: 02 Feb 2021 07:59
by penpen
Could you provide a sample file, so i can see all the special characters (or is the exclamation mark ('!') the only one where the above fails); now that i think about it a leading colon character (':') in a line should also fail.
Also some strings are impossible to replace using environment variables string replacement functionality (for example string starting with an asterisk ('*')).
Inserting new lines depend on the content of your files; there's no way to do that for any content, so i need to know the exact structure of your text files.
However the following should fix the issue with exclamation marks and leading colon characters:
Code: Select all
@echo off
setlocal enableExtensions disableDelayedExpansion
set "replace=something"
set "replaced=different"
set "source=Source.txt"
set "target=Target.txt"
(
for /F "tokens=* delims=0123456789" %%a in ('findstr /N "^" "%source%"') do (
set "line=%%a"
setlocal enableDelayedExpansion
set "line=!line:~1!"
if defined line (
set "line=!line:%replace%=%replaced%!"
)
echo(!line!
endlocal
)
) >"%target%"
goto :eof
penpen
Edit: I just noticed some bugs.
To prevent false positives starting with the double colon character (':'), first remove the double colon added by "findstr /N ...".
To allow files with spaces the filenames should be encapsulated in doublequotes, when used.