title: How to find and replace a string in %PATH% system variable with variables in a batch file?
body: <p>I am trying to upgrade Java with a batch file, and I need to change the PATH system variable to reflect that change. At the beginning of the PATH variable I have</p>
  
  <pre><code>C:\Program Files\Java\jdk1.8.0_51;...
  </code></pre>
  
  <p>I need to change the jdk value to be jdk1.8.0_60. I'm relatively new to command line and batch files, and so I may be misunderstanding something. Here is what I am trying.</p>
  
  <p>I have a couple variables</p>
  
  <pre><code>jVersion=1.8.0_
  javaPath=C:\Program Files\Java
  newVersion=60
  oldVersion=51
  </code></pre>
  
  <p>I found something about replacing strings with literal values like so</p>
  
  <pre><code>set PATH=%PATH:1.8.0_51=1.8.0_60%
  </code></pre>
  
  <p>but I can't get it to work with variables...</p>
  
  <pre><code>set PATH=%%PATH:%jVersion%%oldVersion%=%jVersion%%newVersion%%%
  </code></pre>
  
  <p>I don't know if you need 2 %'s around the outside, or just one, or !'s. I'm not super confident in my knowledge of delayed expansion. I also don't know if this is possible.</p>
  
  <p>As a bonus, I would really like to be able to take whatever comes after ...\Java\ and replace it with my new value. This would be just in case I don't know the value in the PATH variable for the jdk</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT</strong>: 
  By using the command <code>call</code> before a modified version of my code I was able to get it to work</p>
  
  <pre><code>call set PATH=%PATH:%jVersion%%oldVersion%=%jVersion%%newVersion%%
  </code></pre>
  
  <p>I'm still trying to figure out how to make it generic and change whatever comes after <code>...\jdk</code> to the values I have.</p>