views:

1542

answers:

3

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?

+1  A: 

You can do this using vbscript

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

For Each objDrive in colDrives
    Wscript.Echo "Drive letter: " & objDrive.DriveLetter
Next

And you can map network drives with

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive "G:", "\\Server\Path"
u07ch
Your first script would list all the drives on the PC, including any local drive, not just the mapped network drives.
Joel Gauvreau
You could check objDrive.DriveType to ensure it is a network drive (DriveType=3).
Joel Gauvreau
+2  A: 

To list the mapped drives

Net Use

To map a new drive

Net Use G: \\Server\Path
Joel Gauvreau
+2  A: 

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. ;-)

Tomalak
Wow! are you a coding android sent from the future to save/destroy us all? :)
Joel Gauvreau
No. This assumption is wrong on at least one account. ;-) In fact I've never done anything like this in a batch script before, but hey - it seems to work. :)
Tomalak
I'm afraid I'm with Joel here....you are out to destroy us all....man!, that dos environment really has some legs.
Keng
BTW: Tamalak is your avatar reversed by accident? or are you against hackers?
Keng
@Keng: It's not much uglier than Perl, I would say (even though less capable). But that's just an opinion. ;-) I would not say my avatar is reversed. The glider is stable in any direction, so I don't think orientation matters here.
Tomalak

related questions