0

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.

1
  • Thanks all for your answers, but none of them helped me. Let me explain. I have a properties file where I have a property named installPath and while running the .bat file from command line, I am accepting C:\Programfiles\Install\ as input from user and then in my properties file I have to replace installPath with the user input path. All solutions provided here works for hard coded values not for the ones which user gives as input at runtime. Commented Feb 1, 2013 at 5:46

4 Answers 4

1

This works fine for me

set p=installPath
set p=%p:installPath=C:\Programfiles\Install\%
echo %p%
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Perhaps this tool might help you:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.