title: Batch File - Get Sub Path By Subtracting One String Variable From Another?
body: <p>I am writing a batch file that stores some root path (without the file name) in one variable. For example:</p>
  
  <pre><code>set ROOT="C:\Program Files(x86)\Some Program\"
  </code></pre>
  
  <p>In another part of the batch file I am iterating through all the EXE files contained in the root path and all it's subdirectories:</p>
  
  <pre><code>pushd !ROOT!
  for /R "." %%F in (*.exe) do (
  ...
  )
  </code></pre>
  
  <p>So, for example, lets say the program directory structure contains the following:</p>
  
  <pre><code>C:\Program Files(x86)\Some Program\prog.exe
  C:\Program Files(x86)\Some Program\bin\run.exe
  C:\Program Files(x86)\Some Program\bin\help\tuturial.exe
  C:\Program Files(x86)\Some Program\lib\xfg.exe
  </code></pre>
  
  <p>Then the values for %%F in the for loop will be these 4 fully qualified paths.</p>
  
  <p>What I want to do is get the path, starting from the root, up to the last directory name in %%F. For example, if %%F is:</p>
  
  <pre><code>C:\Program Files(x86)\Some Program\bin\help\tuturial.exe
  </code></pre>
  
  <p>and <em>ROOT</em> is:</p>
  
  <pre><code>C:\Program Files(x86)\Some Program\
  </code></pre>
  
  <p>how do I subtract the two variables to come up with the result:</p>
  
  <pre><code>\bin\help\
  </code></pre>
  
  <p>?</p>
  
  <p>In other words:</p>
  
  <pre><code>     %%F = C:\Program Files(x86)\Some Program\bin\help\tuturial.exe
  - !ROOT! = C:\Program Files(x86)\Some Program\
  = -----------------------------------------------------------------
                                               \bin\help\
  </code></pre>
  
  <p>Thank you,</p>
  
  <p>Jan</p>
  
  <p>P.S. Note that I am using delayed expansion, hence the ! instead of % for variable ROOT.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I am getting really close here. I am able to do the string operation but with one condition, I have to "hard code" the substring that I want to remove. If I try to put the substring in a variable it won't work. Here is my batch file:</p>
  
  <pre><code>set ROOT="C:\Program Files(x86)\Some Program\"
  ....
  pushd !ROOT!
  for /R "." %%F in (*.exe) DO (
      set CURR=%%~dpF
  
      set "X1=!CURR:C:\Program Files(x86)\Some Program\=!"       
  
      set TEMP="C:\Program Files(x86)\Some Program\"
      set "X2=!CURR:!TEMP!=!"
  
      ECHO X1=!X1!
      ECHO X2=!X2!
  )
  </code></pre>
  
  <p>And for the test case where CURR=C:\Program Files(x86)\Some Program\bin\help\tutorial.exe here is the output:</p>
  
  <pre><code>X1=bin\help\
  X2=TEMP
  </code></pre>
  
  <p>So as you can see using a variable to hold the substring doesn't seem to work (I am probably doing something blatantly wrong though.)</p>
  
  <p>Anyway, I will keep trying. :) </p>