I want to change a variable from some\original\path
to some\replaced\path
by doing a find-replace and changing the "original" part in the path.
set x=some\original\path
set y= ????
(y is now some\replaced\path)
How would I go about doing this in a Windows batch file? Note that:
- I want to keep the code flexible so no hardcoding the strings or character indexes
- I am fine with a Powershell solution too. I'm looking for whatever is the simplest, most mantainable and can run on my Windows 7 machine.
Solution:
In batch files you can do string replacements in the %%
interpolation process
set y=%x:original=replaced%
Best Answer
Edit: To answer the clarified question:
If you can ensure that
original
is always the same, and is only in the path once, use:This will replace the first instance of
\original\
with\replaced\
. Testing:Previous answer
To change the second section of the path, you can use:
Testing:
This relies on there being no leading slash. It replaces the text between the first and second slash.
Edit: To explain what
%%
means.%%
is a method to escape the percentage sign. If I wrote the following line it would treat% Green 50%
as a variable. Since that variable is undefined, it will write the following output.What I need to write is:
The following line goes through a few transformations. Here is each step of its transformation.