Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remap local removable drive letters from command line

Posted on 2010-09-13
28
Medium Priority
?
1,715 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 5
  • +3
28 Comments
 
LVL 3

Expert Comment

by:Jofnn
ID: 33662526
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
ID: 33662569
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
ID: 33662595
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Zach2001
ID: 33662651
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
ID: 33664136
0
 
LVL 3

Author Comment

by:Zach2001
ID: 33664445
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
ID: 33664480
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
ID: 33664560
Hi, yup, that's pretty much it in a nutshell :)
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33664685
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 14
ID: 33666552
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 71

Expert Comment

by:Qlemo
ID: 33666874
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 14
ID: 33666923
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 14
ID: 33666986
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 71

Expert Comment

by:Qlemo
ID: 33667091
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
 
LVL 14
ID: 33667131
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 14

Assisted Solution

by:Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed) earned 500 total points
ID: 33667317
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
ID: 33667386
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
ID: 33667424
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 1000 total points
ID: 33667709
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
ID: 33680947
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 71

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 33681528
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 71

Expert Comment

by:Qlemo
ID: 33681573
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
ID: 33682308
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
ID: 33682346
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 14
ID: 33684989
Thanks for the points this is pretty cool that each solution has it's own unique methods =)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33686796
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
ID: 33700089
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 71

Expert Comment

by:Qlemo
ID: 33700357
Your are sooo right, Zach! Sorry for that shift!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

722 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