Solved

Remap local removable drive letters from command line

Posted on 2010-09-13
28
1,664 Views
Last Modified: 2012-05-10
Hi,

We have a load of new Windows 7 machine with those 7in1 USB reader installed.  This is gobbling up a swathe of local drive letters, some of which overlap with our 'standard' net work drive mappings.

Is there a neat way to remap these removable drive letters via login script ?

Eg:

net use g: /d
If exist g: then [[make g: something else]]
net use g: \\server\share

Thanks

z
0
Comment
Question by:Zach2001
  • 9
  • 6
  • 5
  • +3
28 Comments
 
LVL 3

Expert Comment

by:Jofnn
Comment Utility
Hi,

You could use a batch file with the following:

net use d: | find "completed successfully"
if errorlevel 0 net use d: /delete
net use d: \\server\share

Is that what you're looking for?
0
 
LVL 3

Author Comment

by:Zach2001
Comment Utility
Hi, thanks, nah, it's the actual means of moving the local removable drive mapping out of the way - rather than testing it's there or not....

0
 
LVL 3

Expert Comment

by:Jofnn
Comment Utility
HI,

What to you mean "moving the local removable drive mapping out of the way" ??

The code above checks whether it's mapped... if it is, it'll un-map it... The final line then re-maps the drive to your designated \\server\share.

Jonathon
0
 
LVL 3

Author Comment

by:Zach2001
Comment Utility
You can't just unmap a local removable media drive mapping, it has to be done either via Disk Managment or DiskPart.
0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
0
 
LVL 3

Author Comment

by:Zach2001
Comment Utility
Very useful, thanks.  The issue is, finding a neat way to extract the required info from the data returned from the list volume command - piping this into a txt file then running that back into diskpart.

This is tricky as its drive letters I need to be keyed on, not Volume ID's – the association between them will be different across different machines – so I can’t just go changing the drive letters of the Volumes, without first ensuring that volume has the drive letter which is getting in the way…
0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
So is this what you want to do?

1. Determine if drive D: is in use as one of the removable drives on the 7in1
2. Move that assignment to drive X: (available drive letter not used in the login script)
3. Assign drive D: according to login script.

This will prevent losing access to the removable drive while maintaining drive integrity through the login script.
0
 
LVL 3

Author Comment

by:Zach2001
Comment Utility
Hi, yup, that's pretty much it in a nutshell :)
0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
Take a look at this:  http://snipplr.com/view/19276/get-list-of-drive-letters-and-labels/

I am a bit tied up to actually work it out but I think you can piece everything together from the two links I gave you.  If you need further assistance let me know.
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
first off you don;t need to key off of a serial number you can key off a drive letter that might be all you need.  i started to write the script which will key off the serial number and began playing with Diskpart and found it has some neat abilities, like you can list all the volumes and just use that list so my script which checks every letter may not be needed...  

However one thing I've noticed is virtual CDROMs like those made by PowerISO and WinImage cannot be located by Diskpart, therefore they cannot be manipulated by Diskpart... if this is not a problem then we're OK.


Here is how to write your diskpart script to use only drive letters:

Select Volume ZRemoveAssign Letter XExit

0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Did you think of the new feature of W7 to only show drives which are indeed containing a media? It was especially invented for those one-of-a-million readers ... It can be found in the Folder Options, View, "Hide empty drives in the Computer folder", and should be checked.
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
okay so here is a partial script, I did more than this but I wanted to pst this much because you can just use this to compare your list of local drives per mapping attempt.  I was starting to create a loop that will go through all your drives you plan to map to compare them, and all that is far beyond your initial scope, I assume you may have done all of that already.

ECHO OFF
SET "LocalDriveList=EMPTY"
SET "DiskPartFile=C:\DiskPartFile\Tmp.dat"
MD "C:\DiskPartFile\"

::- Creates a lisat of drives on your computer to compare against
FOR /F "Tokens=3 skip=8" %%V IN ('ECHO List Volume ^| Diskpart ') DO SET "TD=%%V"&CALL :Start-Make-Drive-List
SET "LocalDriveList=%LocalDriveList:EMPTY =%"

Open in new window

0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
you can compare a single drive as:

ECHO "%LocalDriveList%" | FIND "T:" > nul && ECHO Select Volume %%v>>"DiskPartFile"&ECHO Remove %%v>>"DiskPartFile"&ECHO Assign Letter X

However the thing is making the list of the available drives, this needs to be all the drives NOT currently in use and NOT being mapped by your script.

you will then need to iterate through this list and remove an item each time you assign it to one of the other existing drives.

Then you will also need to handle the possibility there are too many drives.  How do you want to handle that?  or are you certain it woudl never be the case?  (I'm sure a few of my servers coudl have this issue)

obviously before you do anything you should un-map everything currently mapped on the computer using:
NET USE * /D /Y
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Another idea to prevent from mapping drives: Change the drive letters to paths. You can do so once for any PC, creating e.g. a "c:\CardReader" folder with 7 subfolders (e.g. "SDCard"), and mapping each card reader slot to one of those subfolders. No drive letter conflict anymore.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
Qlemo, that is a terrific Idea!  I think that is definitely the best way to solve these issues!  Thank you vMuch Sir!  that will make the script FAR Simpler!
0
 
LVL 11

Assisted Solution

by:Ben Personick
Ben Personick earned 125 total points
Comment Utility
And here we are!  Qlemo you are a genius! ^^

ECHO OFF
SET "LocalDriveList=EMPTY"
SET "RemapRoot=C:\DiskPart\Remount"
SET "LogDir=C:\DiskPart\Logs"
SET "DiskPartFile=C:\DiskPart\Tmp.dat"

MD "%RemapRoot%\"
MD "%LogDir%\"
::- Creates a lisat of drives on your computer to compare against
FOR /F "Tokens=3 skip=8" %%V IN ('ECHO List Volume ^| Diskpart ') DO SET "TD=%%V"&CALL :Start-Make-Drive-List
SET "LocalDriveList=%LocalDriveList:EMPTY =%"

::- In this variable set the list of drive letters you will be mapping:
SET "MapDriveList=A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z"

::- Here we loop through the list of Drives you are mapping looking to see if any are already used and create a temp file to hold the remap script.
IF EXIST "%DiskPartFile%" DEL /F /Q "%DiskPartFile%"
ECHO.REM BeginFile > "%DiskPartFile%"
FOR %%v IN (%MapDriveList%) DO ECHO "%LocalDriveList%" | FIND /I "%%v:" > nul && ECHO Select Volume %%v>>"%DiskPartFile%"&ECHO Remove %%v>>"DiskPartFile&MD "%RemapRoot%\%%v"&ECHO Assign Mount "%RemapRoot%\%%v">>"%DiskPartFile%"
ECHO Exit>>"%DiskPartFile%"

::- Here we re-map the drives to the folders we ccreated int he last step uing the script file we created innt he last step.
ECHO DiskPart /S "%DiskPartFile%"
IF EXIST "%DiskPartFile%" DEL /F /Q "%DiskPartFile%"

GOTO End-Make-Drive-List
:Start-Make-Drive-List
IF /I "%TD:~1,1%"=="" SET "LocalDriveList=%LocalDriveList% "%TD%:""
GOTO :EOF
:End-Make-Drive-List

Open in new window

0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
WOW - I got busy with work (we have auditors here) and couldn't actually write anything here.  But then it looks like anything I might have come up with would have been over complicated as usual. Always happy to learn new ways so thanks for helping out here you guys.  The links that I gave would have pointed to one method but probably not as easy as yours.

:)
0
 
LVL 3

Author Comment

by:Zach2001
Comment Utility
This all looks awsome, cheers guys - I'll have a good peek in the AM after some kip.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
Comment Utility
Hi, here's a VBScript that I use with my new PCs that have a 4 in 1 card reader assigned to E, F, G, and H.

This script will change their drive letters (using the MountVol command) to the second parameter passed in the ChangeDriveLetterWithMountvol procedure.

It needs Admin rights.

Regards,

Rob.
Call ChangeDriveLetters



Sub ChangeDriveLetters

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colVolumes = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=2")

For Each objDisk In colVolumes

    If objDisk.DeviceID = "E:" Then ChangeDriveLetterWithMountVol "E:", "w:"

    If objDisk.DeviceID = "F:" Then ChangeDriveLetterWithMountVol "F:", "X:"

    If objDisk.DeviceID = "G:" Then ChangeDriveLetterWithMountVol "G:", "Y:"

    If objDisk.DeviceID = "H:" Then ChangeDriveLetterWithMountVol "H:", "Z:"

Next

End Sub



' Change the drive in one drive letter to another drive letter using

' mountvol.exe tool

'

Sub ChangeDriveLetterWithMountvol(strSourceDrive, strTargetDrive) 



    Dim objShell, objExec

    Dim strVolume

    

    Set objShell = WScript.CreateObject("WScript.Shell")

    

    ' Get volume associated to the old drive letter.

    '

    Set objExec = objShell.Exec("mountvol " & strSourceDrive & " /L")

    strVolume = Trim(objExec.StdOut.ReadLine())

    while objExec.Status = 0

        WScript.Sleep(100)

    Wend

    

    ' Unmount the drive.

    '

    Set objExec = objShell.Exec("mountvol " & strSourceDrive & " /D")

    while objExec.Status = 0

        WScript.Sleep(100)

    Wend

    

    ' Mount the drive on the new drive letter.

    '

    Set objExec = objShell.Exec("mountvol " & strTargetDrive & " " & strVolume)

    while objExec.Status = 0

        WScript.Sleep(100)

    Wend



End Sub

Open in new window

0
 
LVL 3

Author Comment

by:Zach2001
Comment Utility
Rob, you legend. MountVol is the hidden gem I was searching for !!

Although I can't make your VBS work with Windows 7 (It just returns 0 and does squat) I have knocked up a very ugly BatchScript which I can deploy at logon, to be run as admin.

I'm no programmer! What do you think, have I made some awful oversight here – I have attempted to make it human readable too, but I suppose a loop could be created with a counter for the remappings (?)

It seems to work very well and is surprisingly fast.



@echo off

REM	Set Drive Letters to be remapped here - Add Remove as Neccessary

echo Setting Variables

Set DriveVar1=i:

Set DriveVar1New=h:

Set DriveVar2=m:

Set DriveVar2New=i:

Set DriveVar3=n:

Set DriveVar3New=j:

Set DriveVar4=o:

Set DriveVar4New=k:



REM	Remap Drive Letter 1 - Add Remove as Neccessary

echo Re-Mounting Drive1

FOR /F "tokens=1 delims= " %%A IN ('mountvol %%DriveVar1%% /l') DO SET TempVar=%%A

if NOT (%TempVar%) == (The) (

mountvol %DriveVar1% /D

mountvol %DriveVar1New% %TempVar% 

) 



REM	Remap Drive Letter 2 - Add Remove as Neccessary

echo Re-Mounting Drive2

FOR /F "tokens=1 delims= " %%A IN ('mountvol %%DriveVar2%% /l') DO SET TempVar=%%A

if NOT (%TempVar%) == (The) (

mountvol %DriveVar2% /D

mountvol %DriveVar2New% %TempVar% 

) 



REM	Remap Drive Letter 3 - Add Remove as Neccessary

echo Re-Mounting Drive3

FOR /F "tokens=1 delims= " %%A IN ('mountvol %%DriveVar3%% /l') DO SET TempVar=%%A

if NOT (%TempVar%) == (The) (

mountvol %DriveVar3% /D

mountvol %DriveVar3New% %TempVar% 

) 



REM	Remap Drive Letter 4 - Add Remove as Neccessary

echo Re-Mounting Drive4

FOR /F "tokens=1 delims= " %%A IN ('mountvol %%DriveVar4%% /l') DO SET TempVar=%%A

if NOT (%TempVar%) == (The) (

mountvol %DriveVar4% /D

mountvol %DriveVar4New% %TempVar% 

) 



REM	Clear Variables

echo Clearing Variables

SET TempVar=

Set DriveVar1=

Set DriveVar1New=

Set DriveVar2=

Set DriveVar2New=

Set DriveVar3=

Set DriveVar3New=

Set DriveVar4=

Set DriveVar4New=

Open in new window

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
Comment Utility
One way to make it "nicer" is to use "subprocedures". I have removed everything unnecessary to make it slim.
@echo off

REM     Set Drive Letters to be remapped here - Add Remove as Neccessary

call :Remap i h

call :Remap m i

call :Remap n j

call :Remap o k



exit /b 0





REM ----- Don't change this -------

:Remap

echo Remap Drive Letter %0 to %1

FOR /F "tokens=1 delims= " %%A IN ('mountvol %0: /l') DO if NOT "%%A" == "The" (

  mountvol %0: /D

  mountvol %1: %%A

)

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
One flaw of that script is that if a drive is not mapped on log in, it will not be remapped at all - shouldn't that be forced? E.g. in the first case, h: will only be mapped if i: is mapped.
0
 
LVL 3

Author Comment

by:Zach2001
Comment Utility
That's ace, thanks Qlemo.

This script is to be used to shunt physical drive mappings out the way so that network drives can take there place.  Before this runs, there will be the usual net use  /d and afterwards the usual net use \\server\share

Dunno what the best way to share the points is, but will do my best.

Thanks everybody
0
 
LVL 3

Author Closing Comment

by:Zach2001
Comment Utility
There are three ways here to do what I needed, the neatest is a batch file using mountvol - at least for my environment.
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
Thanks for the points this is pretty cool that each solution has it's own unique methods =)
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Thanks for the grade.  I haven't tried the VBS on Windows 7, but I dare say you'd need to run it elevated.  You could use the Elevate powertoy via
elevate.exe wscript.exe remapphysicaldrives.vbs

and it should work.

Regards,

Rob.
0
 
LVL 3

Author Comment

by:Zach2001
Comment Utility
QLEMO:

The passed paremters should be 1 & 2 eg:


@echo off

REM     Set Drive Letters to be remapped here - Add Remove as Neccessary

call :Remap e j

call :Remap f k

call :Remap g l

call :Remap h m

call :Remap i n



exit /b 0





REM ----- Don't change this -------

:Remap

echo Remap Drive Letter %1 to %2

FOR /F "tokens=1 delims= " %%A IN ('mountvol %1: /l') DO if NOT "%%A" == "The" (

  echo Remap Drive Letter %1 to %2

  mountvol %1: /D

  mountvol %2: %%A

)

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Your are sooo right, Zach! Sorry for that shift!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now