Saurabh Agrawal
Saurabh Agrawal

Reputation: 1365

Batch Command to replace text in file

I know this question is asked many times, but I didn't get the answer for what I am searching. I want to replace a pattern using windows .bat file.

I know how to replace X with Y.

But I am trying to replace say installPath with C:\Programfiles\Install\.

Here, I am facing issues as the new value string contains \ i.e special character.

Please let me know how I can replace this.

Upvotes: 0

Views: 5954

Answers (4)

user2033427
user2033427

Reputation: 873

This should work... By the way, this is my first post on this website. The following uses delayed expansion so that you have two different 'variable symbols' to play with:

    setlocal enabledelayedexpansion
    set iPath=installPath
    set input=C:\Programfiles\Install\
    set p=!iPath:installPath=%input%!

Hope this helps

Upvotes: 0

Shirulkar
Shirulkar

Reputation: 484

Followinf script will find the string in the file and replace with another string. EX. "installPath" will be replaced with "C:\Programfiles\Install"

@echo off
for /f "usebackq tokens=*" %%a in ("test.txt") do call :Replace "%%a"
del "test.txt" 
rename "newfile.txt" test.txt 

exit /b

:Replace
set str1=%~1 
set str1=%str1:installPath=C:\Programfiles\Install% 
echo.%str1%>>"newfile.txt" 

exit /b

Upvotes: 1

Bali C
Bali C

Reputation: 31231

This works fine for me

set p=installPath
set p=%p:installPath=C:\Programfiles\Install\%
echo %p%

Upvotes: 1

user1916076
user1916076

Reputation: 145

Perhaps this tool might help you:

http://sourceforge.net/projects/fart-it/

Upvotes: 0

Related Questions