bat file to add/remove a drive letter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
LMHmedchem
Posts: 7
Joined: 25 Aug 2013 23:30

bat file to add/remove a drive letter

#1 Post by LMHmedchem » 22 Nov 2013 12:21

Hello again,

Lately, I have been keeping my internal backup partition unmounted. This means that I have gone into the disk manager and removed the drive letter. I would like to modify my backup script to re-assign a drive letter to the backup drive, run the backup, and then remove the drive letter.

Suggestions would be greatly appreciated.

LMHmedchem

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: bat file to add/remove a drive letter

#2 Post by penpen » 22 Nov 2013 13:07

This links may help you (sorry not much time today, but should be self explaining):
http://wiki.win-lite.de/index.php?title=Allgemein:Tippsammlung/Laufwerksbuchstaben_zuordnen (german only)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365248(v=vs.85).aspx

penpen

Edit: Added "(german only)", similar informations can be found using the links in my next post (2 links per Posts only).
Last edited by penpen on 22 Nov 2013 17:24, edited 1 time in total.

LMHmedchem
Posts: 7
Joined: 25 Aug 2013 23:30

Re: bat file to add/remove a drive letter

#3 Post by LMHmedchem » 22 Nov 2013 15:53

Sorry, I don't read German very well. Is there an English translation of that page? I can probably follow along well enough to figure this out, but it will be easier in English.

Thanks,

LMHmedchem

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: bat file to add/remove a drive letter

#4 Post by penpen » 22 Nov 2013 17:12

Argh, BIG SORRY, i haven't notice it; i hope this is better:
Using Diskpart.exe: http://www.lascon.co.uk/Windows-Volume-Management.php
Using Mountvol.exe: http://technet.microsoft.com/en-us/library/cc772586(v=ws.10).aspx

Mountvol.exe should suffice to (un)mount the hdd, but diskpart.exe is needed to find out the GUID volume Name.

penpen

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: bat file to add/remove a drive letter

#5 Post by carlos » 22 Nov 2013 23:38

I remember write a script in 2009 that use mountvol for do the task, but i delete it for avoid problems for a possible damage using the script. I remember that was possible unmount c: even using that causes serious problems, forcing to reset.

First, you need get the volume name of what you need mount / unmount.

For example: If I run mountvol I get this:

Code: Select all

C:\>mountvol
Crea, suprime o lista un punto de montaje de volumen.

MOUNTVOL [unidad:]ruta Nombre_Volumen
MOUNTVOL [unidad:]ruta /D
MOUNTVOL [unidad:]ruta /L

    ruta        Especifica el directorio NTFS en el que se establecerá
                el punto de montaje.
    volumen     Especifica el nombre de volumen que será el destino del
                punto de montaje.
    /D          Quita el punto de montaje de volumen del directorio
                especificado.
    /L          Lista el nombre de volumen montado para el directorio
                especificado.

Los valores posibles para Nombre_Volumen junto con los actuales puntos de montaj
e son:

    \\?\Volume{a9f1bd02-fb62-11e2-bbf5-806d6172696f}\
        C:\

    \\?\Volume{a9f1bd00-fb62-11e2-bbf5-806d6172696f}\
        D:\

    \\?\Volume{f31ca7ce-4ff2-11e3-b6fb-080027c713cc}\
        G:\


I know that G:\ is a removable drive, then I will save the volume name of it:
Note: in this: If we save inside variable is better remove the backslashes and copy all from Volume to }

Code: Select all

Volume{f31ca7ce-4ff2-11e3-b6fb-080027c713cc}


code that unmount if it is mounted, and mount if it is unmounted.
note: use under your own responsability. I'm not responsable of possible damage or other problem.

Code: Select all

@echo off

set "volume=Volume{f31ca7ce-4ff2-11e3-b6fb-080027c713cc}"
set "drive="
set "unused="

for %%D in (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) do (
mountvol %%D: /L >nul
if errorlevel 1 (
rem set free letter
set "free=%%D"
) else (
rem set the letter that use the volume
mountvol %%D: /L | findstr "%volume%" >nul
if not errorlevel 1 set "drive=%%D"
)
)

if not defined drive (
echo the volume is not mounted
) else (
echo the volume is mounted in %drive%
)

::unmount
if defined drive (
echo unmounting
mountvol %drive%: /D
)

::mount
if not defined drive (
if not defined free (
echo is not possible mount the volume because there are not a letter free
) else (
echo mounting in %free%:
mountvol %free%:\ \\?\%volume%\
)
)

pause


I test in my pc, and works.

Post Reply