Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1728
  • Last Modified:

Remap local removable drive letters from command line

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
Zach2001
Asked:
Zach2001
  • 9
  • 6
  • 5
  • +3
3 Solutions
 
JofnnCommented:
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
 
Zach2001Author Commented:
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
 
JofnnCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Zach2001Author Commented:
You can't just unmap a local removable media drive mapping, it has to be done either via Disk Managment or DiskPart.
0
 
pony10usCommented:
0
 
Zach2001Author Commented:
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
 
pony10usCommented:
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
 
Zach2001Author Commented:
Hi, yup, that's pretty much it in a nutshell :)
0
 
pony10usCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
pony10usCommented:
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
 
Zach2001Author Commented:
This all looks awsome, cheers guys - I'll have a good peek in the AM after some kip.
0
 
RobSampsonCommented:
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
 
Zach2001Author Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
Zach2001Author Commented:
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
 
Zach2001Author Commented:
There are three ways here to do what I needed, the neatest is a batch file using mountvol - at least for my environment.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Thanks for the points this is pretty cool that each solution has it's own unique methods =)
0
 
RobSampsonCommented:
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
 
Zach2001Author Commented:
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
 
QlemoC++ DeveloperCommented:
Your are sooo right, Zach! Sorry for that shift!
0

Featured Post

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!

  • 9
  • 6
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now