Computer Hope

Microsoft => Microsoft DOS => Topic started by: robtem on August 23, 2007, 05:18:26 AM

Title: How to find and replace a string in text file using batch file
Post by: robtem on August 23, 2007, 05:18:26 AM
I want to write a batch file which can look through a textfile and find a string and replace it with another string. I saw some posting in a forum regarding this with the solution below, but it wont work if a text file contain special characters like <>.

set txtfile=D:\test.txt
set newfile=D:\new_test.txt
if exist "%newfile%" del /f /q "%newfile%"
set /p search=Search String=
set /p replace=Replace With=
for /f "tokens=*" %%a in (%txtfile%) do (
   set newline=%%a
   call set newline=%%newline:%search%=%replace%%%
   call echo %%newline%% >>%newfile%
)
Please help
Title: Re: How to find and replace a string in text file using batch file
Post by: ghostdog74 on August 23, 2007, 05:38:10 AM
show how your input file looks like. the string you want to replace, the expected output. its better this way
Title: Re: How to find and replace a string in text file using batch file
Post by: DeltaSlaya on August 23, 2007, 06:01:04 AM
I don't think you can use < and >'s in batch, you can't evaluate them at least.
Title: Re: How to find and replace a string in text file using batch file
Post by: robtem on August 23, 2007, 06:06:26 AM
input looks like

<Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

expected
<License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

so, basically it replace a word Contents with License
Title: Re: How to find and replace a string in text file using batch file
Post by: robtem on August 23, 2007, 06:10:12 AM
So DeltaSlaya, you mean we cant find and replace a string in something like .xml file?
Title: Re: How to find and replace a string in text file using batch file
Post by: ghostdog74 on August 23, 2007, 07:41:33 AM
input looks like

<Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

expected
<License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

so, basically it replace a word Contents with License
then just replace the word Contents with License without including the "<"....it should work..
Title: Re: How to find and replace a string in text file using batch file
Post by: Sidewinder on August 23, 2007, 07:56:31 AM
You can use the < character or any other special character provided you use the ^ (escape) character:

Code: [Select]
@echo off
setlocal enabledelayedexpansion
set txtfile=D:\test.txt
set newfile=D:\new_test.txt
if exist "%newfile%" del /f /q "%newfile%"
set /p search=Search String=
set /p replace=Replace With=
for /f "tokens=*" %%a in (%txtfile%) do (
   set newline=%%a
   set newline=!newline:%search%=%replace%!
   echo !newline! >> %newfile%
)

Enter the search string as ^<Contents and the replace string  as ^<License

Good luck.  8)
Title: Re: How to find and replace a string in text file using batch file
Post by: robtem on August 23, 2007, 09:11:55 AM
You can use the < character or any other special character provided you use the ^ (escape) character:

Code: [Select]
@echo off
setlocal enabledelayedexpansion
set txtfile=D:\test.txt
set newfile=D:\new_test.txt
if exist "%newfile%" del /f /q "%newfile%"
set /p search=Search String=
set /p replace=Replace With=
for /f "tokens=*" %%a in (%txtfile%) do (
   set newline=%%a
   set newline=!newline:%search%=%replace%!
   echo !newline! >> %newfile%
)

Enter the search string as ^<Contents and the replace string  as ^<License

Good luck.  8)

The problem here will be at the line set newline=%%a, it wont set it because of special characters
Title: Re: How to find and replace a string in text file using batch file
Post by: Sidewinder on August 23, 2007, 09:35:19 AM
I have been unable to reproduce your problem. I changed the file paths but everything else is as posted:

Code: [Select]
d:\wfc\testlib>replace
Search String=^<Contents
Replace With=^<License

d:\wfc\testlib>type test.txt
<Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

d:\wfc\testlib>type new_test.txt
<License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

Did you use the code that was posted? Can we see the results? It might be easier to switch to VBScript.

 8)
Title: Re: How to find and replace a string in text file using batch file
Post by: ghostdog74 on August 23, 2007, 09:46:36 AM
there, a vbscript for you...
Code: [Select]
Option Explicit
Dim objFSO,objFile,myFile,line,toSearch,toReplace
Set objFSO=CreateObject("Scripting.FileSystemObject")
myFile="C:\temp\test.txt"
toSearch = "Contents"
toReplace = "License"
Set objFile=objFSO.OpenTextFile(myFile,1)
Do Until objFile.AtEndOfLine
    line = objFile.ReadLine
If InStr(1,line,toSearch) > 0 Then
line = Replace(line,toSearch,toReplace)
WScript.Echo line
Else
WScript.Echo line
End If
Loop
Set objFSO=Nothing
Set objFile =Nothing
output:
Code: [Select]
C:\vbscript>more c:\temp\test.txt
xxx:yyy:mmm:nnn:ooo:zzz:
aaa:bbbmmm:nnn:ooo::ccc:
mmm:nnn:ooo:
computername =computer1
<Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>
mmm:mmm:nnn:ooo:nnn:ooo:

C:\vbscript>cscript /nologo test2.vbs
xxx:yyy:mmm:nnn:ooo:zzz:
aaa:bbbmmm:nnn:ooo::ccc:
mmm:nnn:ooo:
computername =computer1
<License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>
mmm:mmm:nnn:ooo:nnn:ooo:

to pipe to a new file, use  >
Title: Re: How to find and replace a string in text file using batch file
Post by: robtem on August 23, 2007, 10:13:52 AM
I have been unable to reproduce your problem. I changed the file paths but everything else is as posted:

Code: [Select]
d:\wfc\testlib>replace
Search String=^<Contents
Replace With=^<License

d:\wfc\testlib>type test.txt
<Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

d:\wfc\testlib>type new_test.txt
<License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

Did you use the code that was posted? Can we see the results? It might be easier to switch to VBScript.

 8)

Sorry, this works. but for some reason when I set it directly using
set search=^<Contents
set replace=^<License

it output "The system cannot find the file specified." in the command console, and it generate the output file which is exactly as the source file (didnt replace). What is the difference between setting directly and prompting for search and replace?

Title: Re: How to find and replace a string in text file using batch file
Post by: Fen_Li on August 23, 2007, 11:20:43 AM
Quote
it output "The system cannot find the file specified." in the command console

try use doublequote (") to set variable contain special char..
set "search=^<Contents"
set "replace=^<License"

Title: Re: How to find and replace a string in text file using batch file
Post by: Sidewinder on August 23, 2007, 12:21:26 PM
If you want to do it directly, you don't need the search and replace set statements. Embed the arguments directly in the code:

Code: [Select]
@echo off
setlocal enabledelayedexpansion
set txtfile=D:\wfc\testlib\test.txt
set newfile=D:\wfc\testlib\new_test.txt
if exist "%newfile%" del /f /q "%newfile%"
for /f "tokens=*" %%a in (%txtfile%) do (
   set newline=%%a
   set newline=!newline:^<Contents=^<License!
   echo !newline! >> %newfile%
)

Learning VBScript on a Windows machine makes more sense than messing around with all these crazy symbols used in the command environment....check out the ScriptCenter (http://www.microsoft.com/technet/scriptcenter/default.mspx) for more details.

Just a thought.  8)
Title: Re: How to find and replace a string in text file using batch file
Post by: robtem on August 24, 2007, 03:27:27 AM
The reason i didnt use vb script is what i am trying to do will not support that. but it works fine now by applying the last posting. I appreciate
Title: Re: How to find and replace a string in text file using batch file
Post by: deepaib123 on May 17, 2018, 12:06:51 PM
I wanted to replace "_Pragrma ("Some string")" in .c files in code.
please can anyone help me with this?
Title: Re: How to find and replace a string in text file using batch file
Post by: DaveLembke on May 17, 2018, 02:12:22 PM
deepaib123... You want to dynamically build or edit an already existing .C file and then compile it?

Trying to figure out what your trying to do.
Title: Re: How to find and replace a string in text file using batch file
Post by: patio on May 17, 2018, 02:31:33 PM
May wanna start a new Topic so it recieves the proper attention as this 1 is 11 years old...