title: .bat file to replace network drives
body: <p>I am overseeing a server update, moving network drives from an outdated server to a new one. A large number of relatively tech illiterate employees will need to remap network drives. In order to make the transition as simple as possible, I would like to be able to provide a .bat that will do the following:</p>
  <ol>
  <li>Determine what network drives the user has mapped.</li>
  <li>Determine which of the drives match ones that are to be updated.</li>
  <li>Replace the outdated drives with the new ones.</li>
  <li>Delete the outdated maps.</li>
  </ol>
  <p>I know how to use the &quot;net use&quot; commands to determine the drives, create maps, and delete them. The issue I am running into is that I am unsure how to ensure that each user gets only the network drives that they already had mapped. Some users may not have permissions for all the drives and I don't want to just map all ~10 of them. Most users will only have one or two.</p>
  <p>I would like to keep this as simple as possible, so something built-in to windows would be preferable.</p>
  <hr />
  <p>EDIT:</p>
  <p>The For does return the values I am looking for. Below is some pseudocode that hopefully describes what I want to achieve with this I am uncertain as to how to either put all these commands within the For or how to set variables for the results of every loop.</p>
  <pre><code>@echo off
  echo Finding all \\oldServer drives
  for /F &quot;tokens=2-3 delims= &quot; %%G in ('net use ^|find &quot;\\oldServer&quot;') do @echo &quot;%%G&quot; &quot;%%H&quot; 
  ::if user has oldDrive1 mapped, map newDrive1
  if %%H==\\oldServer\oldDrive1 net use * \\newServer\newDrive1 /persistent:yes
  ::if user has oldDrive1 mapped, remove that map
  if %%H==\\oldServer\oldDrive1 net use %%G /delete
  ::if user has oldDrive2 mapped, map newDrive2
  if %%H==\\oldServer\oldDrive2 net use * \\newServer\newDrive2 /persistent:yes
  ::if user has oldDrive2 mapped, remove that map
  if %%H==\\oldServer\oldDrive2 net use %%G /delete
  pause
  </code></pre>
  <hr />
  <p>EDIT:</p>
  <p>I was able to figure out something that works. It is far from elegant, but it works.</p>
  <pre><code>@echo off
  set old1=\\oldServer\oldDrive1
  set old2=\\oldServer\oldDrive2
  set old3=\\oldServer\oldDrive3
  set old4=\\oldServer\oldDrive4
  set old5=\\oldServer\oldDrive5
  set old6=\\oldServer\oldDrive6
  set old7=\\oldServer\oldDrive7
  set old8=\\oldServer\oldDrive8
  set old9=\\oldServer\oldDrive9
  set new1=\\newServer\newDrive1
  set new2=\\newServer\newDrive2
  set new3=\\newServer\newDrive3
  set new4=\\newServer\newDrive4
  set new5=\\newServer\newDrive5
  set new6=\\newServer\newDrive6
  set new7=\\newServer\newDrive7
  set new8=\\newServer\newDrive8
  set new9=\\newServer\newDrive9
  for /F &quot;tokens=2-3 delims= &quot; %%G in ('net use ^|find &quot;\\oldServer&quot;') do (
      @echo %%G %%H
      if /I %%H==%old1% net use * %new1% /persistent:yes
      if /I %%H==%old2% net use * %new2% /persistent:yes
      if /I %%H==%old3% net use * %new3% /persistent:yes
      if /I %%H==%old4% net use * %new4% /persistent:yes
      if /I %%H==%old5% net use * %new5% /persistent:yes
      if /I %%H==%old6% net use * %new6% /persistent:yes
      if /I %%H==%old7% net use * %new7% /persistent:yes
      if /I %%H==%old8% net use * %new8% /persistent:yes
      if /I %%H==%old9% net use * %new9% /persistent:yes
      net use %%G /delete
  )
  exit
  </code></pre>
  <p>Thank you for your assistance, this would have taken a lot longer without it.</p>
  <hr />
  <p>EDIT:</p>
  <p>The suggestion to move the /delete function was taken, I was unsure if the /delete would reset the %%G and %%H variables, but this works a little cleaner.</p>
  <pre><code>@echo off
  set old1=\\oldServer\oldDrive1
  set old2=\\oldServer\oldDrive2
  set old3=\\oldServer\oldDrive3
  set old4=\\oldServer\oldDrive4
  set old5=\\oldServer\oldDrive5
  set old6=\\oldServer\oldDrive6
  set old7=\\oldServer\oldDrive7
  set old8=\\oldServer\oldDrive8
  set old9=\\oldServer\oldDrive9
  set new1=\\newServer\newDrive1
  set new2=\\newServer\newDrive2
  set new3=\\newServer\newDrive3
  set new4=\\newServer\newDrive4
  set new5=\\newServer\newDrive5
  set new6=\\newServer\newDrive6
  set new7=\\newServer\newDrive7
  set new8=\\newServer\newDrive8
  set new9=\\newServer\newDrive9
  for /F &quot;tokens=2-3 delims= &quot; %%G in ('net use ^|find &quot;\\oldServer&quot;') do (
     @echo %%G %%H
     net use %%G /delete
     if /I %%H==%old1% net use %%G %new1% /persistent:yes
     if /I %%H==%old2% net use %%G %new2% /persistent:yes
     if /I %%H==%old3% net use %%G %new3% /persistent:yes
     if /I %%H==%old4% net use %%G %new4% /persistent:yes
     if /I %%H==%old5% net use %%G %new5% /persistent:yes
     if /I %%H==%old6% net use %%G %new6% /persistent:yes
     if /I %%H==%old7% net use %%G %new7% /persistent:yes
     if /I %%H==%old8% net use %%G %new8% /persistent:yes
     if /I %%H==%old9% net use %%G %new9% /persistent:yes    
  )
  exit
  </code></pre>