Batch file edit text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Beach Boy
Posts: 9
Joined: 27 Jul 2013 10:40

Batch file edit text file

#1 Post by Beach Boy » 28 Jul 2013 11:52

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.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text file

#2 Post by penpen » 28 Jul 2013 12:25

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.
Last edited by penpen on 28 Jul 2013 16:52, edited 2 times in total.

Beach Boy
Posts: 9
Joined: 27 Jul 2013 10:40

Re: Batch file edit text file

#3 Post by Beach Boy » 28 Jul 2013 14:27

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.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text file

#4 Post by penpen » 28 Jul 2013 15:45

Sorry i didn't test it.
I've corrected the code above, and it should work now.

penpen

Beach Boy
Posts: 9
Joined: 27 Jul 2013 10:40

Re: Batch file edit text file

#5 Post by Beach Boy » 28 Jul 2013 16:02

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file edit text file

#6 Post by foxidrive » 28 Jul 2013 16:40

Try it with the values in the bat file. It works, but blank lines give it a hernia. :)

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text file

#7 Post by penpen » 28 Jul 2013 16:56

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

Beach Boy
Posts: 9
Joined: 27 Jul 2013 10:40

Re: Batch file edit text file

#8 Post by Beach Boy » 29 Jul 2013 11:47

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

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text file

#9 Post by penpen » 29 Jul 2013 12:14

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
Last edited by penpen on 29 Jul 2013 12:25, edited 1 time in total.

Beach Boy
Posts: 9
Joined: 27 Jul 2013 10:40

Re: Batch file edit text file

#10 Post by Beach Boy » 29 Jul 2013 12:25

That worked greatly. Thank you for the assistance.

werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Re: Batch file edit text file

#11 Post by werejago » 01 Oct 2020 09:20

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.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file edit text file

#12 Post by aGerman » 02 Oct 2020 02:05

@werejago
Has this been answered? viewtopic.php?f=3&t=9795&p=63004#p62995

Steffen

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text file

#13 Post by penpen » 05 Oct 2020 05:11

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

msswamy
Posts: 2
Joined: 01 Feb 2021 21:06

Re: Batch file edit text file

#14 Post by msswamy » 01 Feb 2021 21:15

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.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text file

#15 Post by penpen » 02 Feb 2021 07:59

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.

Post Reply