Open In App

Batch Script – Replace a String

In this article, we are going to Replace a substring with any given string.

Batch Script :

@echo off 
set str=GFG is the platform for geeks. 
echo %str% 

set str=%str:the=best% 
echo %str%
pause

In the above example, we are going to replace ‘the’ by substring ‘best’ using %str:the=best% statement.



Explanation :

set str=input string

Output :



‘the’ is replaced by ‘best’

Another Approach :

Batch Script :

@echo off 
set str=GFG is the platform for geeks.
set word=best
echo %str% 

call set str=%%str:the=%word%%%
echo %str%
pause

Explanation :

output by 2nd approach

Article Tags :