122

We can replace strings in a batch file using the following command

set str="jump over the chair"
set str=%str:chair=table%

These lines work fine and change the string "jump over the chair" to "jump over the table". Now I want to replace the word "chair" in the string with some variable and I don't know how to do it.

set word=table
set str="jump over the chair"
??

Any ideas?

Péter Török
  • 114,404
  • 31
  • 268
  • 329
Faisal
  • 4,054
  • 6
  • 34
  • 55
  • possible duplicate of [how to replace string inside a bat file with command line parameter string](http://stackoverflow.com/questions/5816178/how-to-replace-string-inside-a-bat-file-with-command-line-parameter-string) which has much more informal answer (it has the same solutions as the answers here though) – Fr0sT Mar 18 '15 at 07:25
  • 1
    now it's not important who was first, but it's good to link identical questions and point to the most irrefragable answer – Fr0sT Mar 25 '15 at 12:26
  • https://cmdshell.blog/2022/02/08/string-replacement-in-a-batch-file/ – Faisal Feb 08 '22 at 08:50

4 Answers4

104

You can use the following little trick:

set word=table
set str="jump over the chair"
call set str=%%str:chair=%word%%%
echo %str%

The call there causes another layer of variable expansion, making it necessary to quote the original % signs but it all works out in the end.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • I like this solution, escaping strings is always problematic in batch files, ENABLEDELAYEDEXPANSION just adds another character to worry about. – Anders May 05 '10 at 19:43
  • 12
    Important thing regarding answer provided by **Joey**. That you need to put the code into batch file for it to work. If you just test it in command line, it will return unexpected `%"jump over the "word%%%`. Just be aware, that code in batch files and in command line may produce different results. – dadhi Apr 23 '14 at 05:42
  • 2
    Building on dadhi's spot-on comment, the solution for command line is here: https://stackoverflow.com/questions/29944902/how-to-replace-with-percent-character-in-cmd-exe. – Kenn Sebesta Jun 16 '17 at 18:46
  • 3
    Upvoting this answer because it works both ways, with the environment variable in either position, or in both the "before" and "after" positions: `set word=table ` `set str="jump over the chair" ` `call set str=%%str:chair=%word%%% ` `echo %str% ` `set word1=chair ` `set word2=desk ` `set str="jump over the chair" ` `call set str=%%str:%word1%=%word2%%% ` `echo %str%' ` – Tom Warfield Sep 17 '17 at 17:51
  • Can be really useful to convert a "local path" to an "admin share" path by prepending \\servername\ and then using this trick to just replace drive letter colon to a $. – Justin Nov 07 '20 at 00:52
  • 1
    trying to use this technique with both string_to_search_in and pattern_to_replace are given in variable `call set str=%%%string%:%pattern%=#%%` but it does not work for me. – user2956477 Jun 19 '22 at 11:46
  • "_making it necessary to **quote** the original % signs_" Where **quote** means **escape** I think? (Or I'm missing something.) – ruffin Jan 06 '23 at 21:14
90

You can use !, but you must have the ENABLEDELAYEDEXPANSION switch set.

setlocal ENABLEDELAYEDEXPANSION
set word=table
set str="jump over the chair"
set str=%str:chair=!word!%
Vicky
  • 12,934
  • 4
  • 46
  • 54
0

I was able to use Joey's Answer to create a function:

Use it as:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

SET "MYTEXT=jump over the chair"
echo !MYTEXT!
call:ReplaceText "!MYTEXT!" chair table RESULT
echo !RESULT!

GOTO:EOF

And these Functions to the bottom of your Batch File.

:FUNCTIONS
@REM FUNCTIONS AREA
GOTO:EOF
EXIT /B

:ReplaceText
::Replace Text In String
::USE:
:: CALL:ReplaceText "!OrginalText!" OldWordToReplace NewWordToUse  Result
::Example
::SET "MYTEXT=jump over the chair"
::  echo !MYTEXT!
::  call:ReplaceText "!MYTEXT!" chair table RESULT
::  echo !RESULT!
::
:: Remember to use the "! on the input text, but NOT on the Output text.
:: The Following is Wrong: "!MYTEXT!" !chair! !table! !RESULT!
:: ^^Because it has a ! around the chair table and RESULT
:: Remember to add quotes "" around the MYTEXT Variable when calling.
:: If you don't add quotes, it won't treat it as a single string
::
set "OrginalText=%~1"
set "OldWord=%~2"
set "NewWord=%~3"
call set OrginalText=%%OrginalText:!OldWord!=!NewWord!%%
SET %4=!OrginalText!
GOTO:EOF

And remember you MUST add "SETLOCAL ENABLEDELAYEDEXPANSION" to the top of your batch file or else none of this will work properly.

SETLOCAL ENABLEDELAYEDEXPANSION
@REM # Remember to add this to the top of your batch file.
-4

This works fine

@echo off    
set word=table    
set str=jump over the chair    
set rpl=%str:chair=%%word%    
echo %rpl%
  • 9
    Sorry, it looks good, BUT it's wrong! It removes the word `chair` and append the word `table`, but it doesn't exchange the two words. Try to replace the word `over` with `under` and you get `jump the chairunder` – jeb Feb 25 '19 at 13:50