title: Wrong sync Folder Mapping in Vagrant with Windows
body: <p>I use Vagrant for Web-Development with a Windows Host.
  However I seem to have a strange problem and Digging all sorts of Google results wont hand me any solution for this.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>I have a Sync Folder defined in windows with this stucture
  D:/VM/tutorial-test/vagrant/VagrantFile
  D:/VM/tutorial-test/vagrant/tutorial/index.php</p>
  
  <p>So the VagrantFile is configured to use the Folder "tutorial" to mount as Sync folder.
  This is in Linux a subfolder of "/var/www"</p>
  
  <p>Half of the time it mounts verything right.
  The subfolder gets mounted and I can add and remove Files.
  The other Half while "vagrant up" is running, it seems like he Mounts a different Windows folder. Its mostly 1 or 2 Folder back in the Tree where my VagrantFile sits. </p>
  
  <p>Here is my VagrantFile:</p>
  
  <pre class="lang-rb prettyprint-override"><code>VAGRANTFILE_API_VERSION = "2"
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "hashicorp/precise32" 
    config.vm.provision :shell, path: "bootstrap.sh" 
    config.vm.provision "shell", path: "after-boot.sh", run: "always"
    config.vm.network :forwarded_port, host: 80, guest: 80
    config.vm.network :private_network, ip: '192.168.50.50'
  
    config.vm.provider "virtualbox" do |v|
      v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
      v.customize ["modifyvm", :id, "--memory", 2048]
      v.customize ["modifyvm", :id, "--cpus", 2]
    end
  
    config.vm.synced_folder "vhosts/", "/etc/apache2/sites-available",
      owner: "root", group: "root"
    config.vm.synced_folder "tutorial/", "/var/www/tutorial",
      owner: "www-data", group: "www-data"
  
  end
  </code></pre>
  
  <p>What happens when I initiate a "vagrant up"</p>
  
  <p><img src="https://i.stack.imgur.com/OKbx6.png" alt="enter image description here"></p>
  
  <p>As you can see, it mounts the "D:/VM" Folder.
  The actual structure here should be D:/VM/tutorial-test/vagrant/tutorial
  There are more than 1 Sync Folder defined. He acts equaly with every Sync Folder.</p>
  
  <p>There must be something, that confuses the Vagrant Setup Script.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I did some more tests. It seems like the Vagrantfile get correctly readed, but the Vagrant Root is going to be 2 levels up in the Windows File tree. </p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>The User Emyl brought me on the Track that maybe my Calling Script is broken. Using the normal Shell is working.
  Because of this im Posting the Bash Script I wrote, since it's Open Source anyway:</p>
  
  <pre><code>@echo off
  
  :: BatchGotAdmin
  :-------------------------------------
  REM  --&gt; Check for permissions
  &gt;nul 2&gt;&amp;1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
  
  REM --&gt; If error flag set, we do not have admin.
  if '%errorlevel%' NEQ '0' (
      echo Requesting administrative privileges...
      goto UACPrompt
  ) else ( goto gotAdmin )
  
  :UACPrompt
      echo Set UAC = CreateObject^("Shell.Application"^) &gt; "%temp%\getadmin.vbs"
      set params = %*:"=""
      echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 &gt;&gt;     "%temp%\getadmin.vbs"
  
      "%temp%\getadmin.vbs"
      del "%temp%\getadmin.vbs"
      exit /B
  
  :gotAdmin
      pushd "%CD%"
      CD /D "%~dp0"
  :--------------------------------------
  
  cls
  dir
  cd %~dp0
  dir
  cd vagrant
  dir
  :start
  echo Please select an Option:
  echo -------------------------
  echo 1: Start Vagrant VM
  echo 2: Reload Vagrant VM
  echo 3: Shutdown Vagrant VM
  echo 4: Suspend Vagrant VM
  echo 5: Destroy Vagrant VM (see Note)
  echo 6: Command Line (for other commands)
  echo 7: Exit
  echo -------------------------
  echo Note: Destroy will uninstall the VM, exept the Files in the Vagrant Folder! &amp;     echo.Please create a Database-Dump before destroying the VM, to prevent Data-Loss.
  echo -------------------------
  set /p choice=Enter a Number: 
  if '%choice%'=='1' goto :up
  if '%choice%'=='2' goto :reload
  if '%choice%'=='3' goto :halt
  if '%choice%'=='4' goto :suspend
  if '%choice%'=='5' goto :destroy
  if '%choice%'=='6' goto :command
  if '%choice%'=='7' goto :end
  echo %Choice% is not a valid option. Please try again.
  echo ...
  goto :start
  :up
  vagrant up
  goto :start
  :reload
  vagrant reload
  goto :start
  :halt
  vagrant halt
  goto :start
  :suspend
  vagrant suspend
  goto :start
  :destroy
  vagrant destroy
  goto :start
  :command
  set /p command=Enter Command: 
  call %%command%%
  goto :start
  :end
  pause
  exit
  </code></pre>
  
  <p><strong>EDIT 3:</strong></p>
  
  <p>The Problem relys on calling the File with a Batch Script.
  The Problem are long Path names which get shortened by Windows like TUTORI~2.
  This seems to break Vagrants Env Variable generation.</p>
  
  <p><strong>EDIT 4 - Solution:</strong></p>
  
  <p>As mentioned I was calling Vagrant with a Batch Script.
  Vagrant doesnt liked the shortened URLS generated by my CMD when using the Location of the current Script with "%~dp0".
  The Solution as described here:</p>
  
  <p><a href="https://stackoverflow.com/questions/24915666/windows-batch-getting-path-without-shortening">Solution</a></p>
  
  <p>With this snippet I am able to create a windows Path. Using this Path will cause Vagrant no Issues anymore and my Sync Folder is generated properly. </p>