Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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%
share|improve this question
    
This is a bit unclear to me do you want to find these paths and change them in a file or actually move some files? –  MaskedPlant Nov 28 '11 at 23:16
    
If you don't want hardcoded strings, what is your criteria to identify the part to change? Do you want to change the second folder in the path while maintaining the first and third folders? –  Hand-E-Food Nov 28 '11 at 23:17
    
@Hand-E-Food: Yes, I want to change the second folder, but if x is some\different\original\path I want to change the third folder and so on. The initial path is not harcoded. The "original -> replaced" replacement is. –  missingno Nov 28 '11 at 23:28
add comment

4 Answers

up vote 1 down vote accepted

Edit: To answer the clarified question:

If you can ensure that original is always the same, and is only in the path once, use:

@Echo Off
Set "Find=original"
Set "Replace=replaced"
Set "OldPath=%~1"
Call Set "NewPath=%%OldPath:\%Find%\=\%Replace%\%%"
Echo %NewPath%

This will replace the first instance of \original\ with \replaced\. Testing:

C:\> ChangePath.bat "Alice\original\Clive"
Alice\replaced\Clive

C:\> ChangePath.bat "Alice\original\Clive\Denver"
Alice\replaced\Clive\Denver

C:\> ChangePath.bat "Alice\Bob\original\Clive"
Alice\Bob\replaced\Clive

Previous answer

To change the second section of the path, you can use:

@Echo Off
Set "Replace=Replacement Path"
Set "PathABC=%~1"
Set "PathBC=%PathABC:*\=%"
Call Set "PathA=%%PathABC:\%PathBC%=%%"
Set "PathC=%PathBC:*\=%"
Set "NewPath=%PathA%\%Replace%\%PathC%"
Echo %NewPath%

Testing:

C:\> ChangePath.bat "Alice\Bob\Clive"
Alice\Replacement Path\Clive

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.

C:\> Echo Red 90% Green 50% Blue 5%
Red 90 Blue 5%

What I need to write is:

C:\> Echo Red 90%% Green 50%% Blue 5%%
Red 90% Green 50% Blue 5%

The following line goes through a few transformations. Here is each step of its transformation.

:: Original line
Call Set "NewPath=%%OldPath:\%Find%\=\%Replace%\%%"

:: The variables encased in single `%` are evaluated:
Call Set "NewPath=%%OldPath:\original\=\replaced\%%"

:: `Call` runs the rest of the line as a command.  The `%%` are evaluated to `%`.
Set "NewPath=%OldPath:\original\=\replaced\%"

:: The search and replace on `OldPath` occurs.
Set "NewPath=Alice\replaced\Clive"

:: The final command is processed.
share|improve this answer
    
Where can I find out what all those %% mean? Im kinda new with batch files –  missingno Nov 29 '11 at 1:12
    
I use this site a lot for batch scripting. Check out the Syntax link at the top of the page as well. I'll edit my answer to explain %%. –  Hand-E-Food Nov 29 '11 at 1:59
add comment

With PowerShell, you can use the -replace operator:

$x = 'some\original\path'
$y = $x -replace 'original','replaced'

The -replace operator uses regular expressions, so you could also do:

$y = $x -replace '\\\w+\\','\replaced\'
share|improve this answer
add comment

Not clear if this is what you want, but in PowerShell, string replacement is quite easy:

$x = "some\original\path"
$y = $x.Replace("original", "replaced")

Should get you what you wanted (I hope).

share|improve this answer
add comment

In Powershell, to replace a string:

$string1 = "blah1\blah2.txt"
$toFind = "blah1"
$toReplace = "blah2"
$string1 = $string1.replace($toFind,$toReplace)

share|improve this answer
    
Just as an FYI, PowerShell has a replace operator, also. The operator can use regular expressions, whereas the Replace() method does not. Example: $string1 = $string1 -replace $toFind, $toReplace –  Patrick S. Nov 28 '11 at 23:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.