title: Forward slash in last argument causes path to directory of batch file ("%~dp0") to change
body: <p>I am learning a bit how to work with parameters in a batch-script and ended up creating some kind of template for reading arguments and setting parameters</p>
  
  <pre><code>@echo off
  SetLocal EnableDelayedExpansion
  set needextra=
  set errstat=
  set noflag=
  set param_f=Not set yet
  set param_g=You didn't use G-flag
  
  :readARGS
  IF [%1] == [] goto :endARGS
  call :arg_%1 2&gt; nul
  IF ERRORLEVEL 1 call :arg_default %1
  SHIFT
  IF DEFINED needextra (
      set %needextra%=%~1
      SHIFT
      set needextra=
  )
  goto :readARGS
  :endARGS
  echo path to directory of batch script: %~dp0
  echo    - noflag:  !noflag!
  echo    - param_f: !param_f!
  echo    - param_g: !param_g!
  EndLocal
  exit /b 0
  </code></pre>
  
  <p>The first echo that prints the directory is important for my question (see later) 
  After that I create a function for each flag (<code>arg_/flag</code>) and one for arguments without a flag (the <code>arg_default</code>):</p>
  
  <pre><code>:arg_/f           -- flag f: set param_f to value of next argument
  set needextra=param_f
  exit /b 0
  
  :arg_/g           -- flag g: just set the param_g to a value
  set param_g=You used the G-flag
  exit /b 0
  
  :arg_default      -- default, neither flag f or g: just set the noflag
  echo noflag=%~1
  exit /b 0
  </code></pre>
  
  <p>When I put everything in a batch-file, let's say <code>C:\Users\user\scripts\params.bat</code> and put the directory in the path I can execute the script:</p>
  
  <pre><code>&gt; params "just an arg"
  path to directory of batch script: C:\Users\user\scritpts\
     - noflag:  just an arg
     - param_f: Not set yet
     - param_g: You didn't use G-flag
  &gt; params another /G /F "this is f"
  path to directory of batch script: C:\Users\user\scritpts\
     - noflag:  another
     - param_f: this is f
     - param_g: You used the G-flag
  </code></pre>
  
  <p>The fact I put it in functions allows me to enter the parameters in whatever order I wish but if I put the G-flag as last I get this strange behaviour:</p>
  
  <pre><code>&gt; params /F "this is f again" bizar /G
  path to directory of batch script: C:\
     - noflag:  bizar
     - param_f: this is f again
     - param_g: You used the G-flag
  </code></pre>
  
  <p>The <code>%~dp0</code> returns only <code>C:\</code>! I tried it with other parameters, moved the batch-file to another directory, called it within the directory, the <code>%~dp0</code> kept returning only <code>C:\</code>. In fact each time the last argument contains a "/" the <code>%~dp0</code> will behave "strangely" like in this example:</p>
  
  <pre><code>&gt; params /G /F stranger "what happens /here"
  path to directory of batch script: C:\Users\user\script\what happens \
     - noflag:  what happens /here
     - param_f: stranger
     - param_g: You used the G-flag
  </code></pre>
  
  <p>Can someone please explain me why this is happening? I cannot figure out why and could not find anything on the web either. I use Windows 10</p>
  
  <p>I really appreciate any help you can provide.</p>