Batch file edit text file
Moderator: DosItHelp
Batch file edit text file
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
In many cases something like this suffices:
penpen
Edit: Corrected error: missing '' in loop
Edit2: Removed 2 bugs on undefined line.
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.
Last edited by penpen on 28 Jul 2013 16:52, edited 2 times in total.
Re: Batch file edit text file
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.
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
Sorry i didn't test it.
I've corrected the code above, and it should work now.
penpen
I've corrected the code above, and it should work now.
penpen
Re: Batch file edit text file
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
Try it with the values in the bat file. It works, but blank lines give it a hernia.
Re: Batch file edit text file
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.
penpen
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
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:
[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
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:
As on my pc the export is done in ANSI i easily forget the Unicode problem, sry for that.
penpen
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
penpen
Last edited by penpen on 29 Jul 2013 12:25, edited 1 time in total.
Re: Batch file edit text file
That worked greatly. Thank you for the assistance.
Re: Batch file edit text file
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.
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
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
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.
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
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:
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.
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
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.