title: Batch get relative path from absolute path
body: <p>How can I convert an absolute path to a relative path in batch? I have an absolute path to a directory <code>A</code> and a reference directory <code>B</code>, and I need the path to <code>A</code> relative to <code>B</code>. As example, the following batch script should print <code>..\other\somedir\</code>.</p>
  
  <pre><code>setlocal enabledelayedexpansion
  set referencePath=C:\Users\xyz\project\
  set absolutePath=C:\Users\xyz\other\somedir\
  set relativePath=...
  echo %relativePath%
  </code></pre>
  
  <p>I tried <code>relativePath=!absolutePath:%referencePath%=!</code>, but this yields the absolute path <code>C:\Users\xyz\other\somedir\</code>.</p>
  
  <p>I need something similar to the python function <code>os.path.relpath</code>:</p>
  
  <pre><code>&gt;&gt;&gt; os.path.relpath("C:\\Users\\xyz\\other\\somedir", "C:\\Users\\xyz\\project\\")
  "..\\other\\somedir"
  </code></pre>
  
  <p>I need this because I have a batch file with command line arguments similar to the above file names. This batch file creates another batch file <code>startup.bat</code> which sets some environment variables and starts an application. The <code>startup.bat</code> may be called over network, so I have to use relative paths. With absolute paths, the environment variables would point to the files on the wrong machine.</p>