Search code examples
batch-filewindows-server-2003drive-mapping

How can I get a drive list from a batch file?


I'm trying to get a list of all my mapped drives for a system upgrade and would like to get this info via a batch file. How can I do this?

For Bonus points: How can I script the mapping of these drives on the new server?


Solution

  • The list of current mappings will be returned by

    net use
    

    The transfer would work like this (for the fun1 of it, lets do that in batch script instead of VBScript):

    @echo off
    
    setlocal EnableDelayedExpansion
    
    set letter=.
    set uncpath=.
    set colon=.
    
    for /f "delims=" %%l in ('net use') do @(
      for /f "tokens=2" %%t in ("%%l") do @set letter=%%t
      for /f "tokens=3" %%t in ("%%l") do @set uncpath=%%t
    
      set colon=!letter:~1,1!
    
      if "!colon!" EQU ":" (
        echo if exist !letter! net use !letter! /delete
        echo net use !letter! !uncpath! /persistent:yes
      )
    )
    
    endlocal
    

    output goes something like this:

    if exist M: net use M: /delete
    net use M: \\someserver\someshare /persistent:yes
    if exist N: net use N: /delete
    net use N: \\otherserver\othershare /persistent:yes
    

    Just store that in a batch file and you are good to go.


    1 Actually, "fun" is not the right word here. ;-)