String replacement in batch file

batch file replace string in filename
batch file replace line in text file
batch file string manipulation
batch string replace special characters
batch file remove characters from string
batch replace character in string
findstr replace text in file
windows batch file replace substring

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?

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!%

Batch Script - Replace a String, Batch Script - Replace a String. To replace a substring with another string use the string substitution feature. 1. Replace string "work" with "play" @echo off set string=This is my string to work with. set 2. Remove the string "to work with." @echo off set string=This is my string to work with. set string=%string:to work 3. Replace all spaces with an underscore @ echo off set string=This is my string

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.

CMD Variable edit replace - Windows CMD, Use the syntax below to edit and replace the characters assigned to a string variable. If the batch parameter was supplied as %2 then this would be: ECHO � This batch allows string substitution in a text file. It parses each line of a text file for a particular string and replaces it with another string. I.e. To replace all occurrences of "Yellow Submarine" in "color.txt" with "uboot" and put the output on the screen run: BatchSubstitute.bat "Yellow Submarine" uboot color.txt Or

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.

String replacement in Windows batch, in that it will NOT build up a list of files in the current directory, but instead will just set the LIST variable to the last file found. Again, this is because the %LIST% is� 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".

This works fine

@echo off    
set word=table    
set str=jump over the chair    
set rpl=%str:chair=%%word%    
echo %rpl%

String substitution and replacing in batch files, To replace one substring with another using details in a variable then you need to turn on delayed expansion and use the variation below. set string=This is my string to work with. Output = This is your replacement to work with. A very useful function which is missing from the Windows library of command line tools is the ability to replace text in plain text files. A function like this can be used for a variety of practical tasks which many system admin’s perform, such as: Update configuration/INI files to replace UNC paths.

DOS - String Manipulation, Description: Similar to the Left function in VB a batch script can return a specified number of characters Remove - Remove a substring using string substitution� Remove all spaces in a string via substitution. Replace: Replace a substring using string substitution. Right String: Extract characters from the end of a string. Split String: Split a String, Extract Substrings by Delimiters. String Concatenation: Add one string to another string. Trim Left: Trim spaces from the beginning of a string via "FOR" command.

How to search & replace a string in text file using a batch file, I want to write a batch file which can look through a textfile and find a string and replace it with another string. This two string will not be� One way to do that is to use the -replace operator. This PowerShell operator finds a string and replaces it with another. Using the example file contents, we can provide the search string foo with the replacement string bar which should make the file contents foo foo baz now. PS> $newContent = $content -replace 'foo', 'bar' bar bar baz

Replace character of string in batch script, batch file remove first character from string command line string replace batch file concatenate quoted strings cmd split string. I need to replace character * from a� Summary: The Scripting Wife learns how to use Windows PowerShell and regular expressions to replace text in strings. Microsoft Scripting Guy, Ed Wilson, here. I just finished a meeting with the Microsoft TechEd people about doing a series of five Live Meetings next week (beginning on March 28) about Windows PowerShell for the Road to TechEd.

Comments
  • possible duplicate of 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)
  • now it's not important who was first, but it's good to link identical questions and point to the most irrefragable answer
  • But what if str itself is from delayed expansion? set str=!str:chair=!word!! does not work correctly.
  • I like this solution, escaping strings is always problematic in batch files, ENABLEDELAYEDEXPANSION just adds another character to worry about.
  • 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.
  • Building on dadhi's spot-on comment, the solution for command line is here: stackoverflow.com/questions/29944902/….
  • 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%'
  • @MubasharAhmad: Isn't that what I've written? Both in the code snippet and the explanation? o.o
  • 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