Skip to content

Instantly share code, notes, and snippets.

@jomifepe
Last active June 12, 2021 15:25
Show Gist options
  • Save jomifepe/cd3f6bd8944612e9059d2e977ec28505 to your computer and use it in GitHub Desktop.
Save jomifepe/cd3f6bd8944612e9059d2e977ec28505 to your computer and use it in GitHub Desktop.
Allows you to mount and unmount drives in Windows by its label
@echo off
setlocal ENABLEDELAYEDEXPANSION
set label=VOLUME_LABEL_HERE
set letter=VOLUME_LETTER_HERE
set dpvns="%TEMP%\dpvn.txt"
set dprls="%TEMP%\dprl.txt"
echo list volume>%dpvns%
echo exit>>%dpvns%
set label_short=%LABEL:~0,11%
for /f "delims=" %%i in ('Diskpart /s %dpvns%') do (
set string=%%i
if not "!string:%label_short%=!"=="!string!" (
set volnum=!string:~9,3!
set volnum=!volnum: =!
)
)
if not "!volnum!"=="" (
echo list volume>%dprls%
echo select volume !volnum!>>%dprls%
:: To assign letter (mount)
echo assign letter=%letter%>>%dprls%
:: To remove letter (unmount)
:: echo remove letter=%letter%>>%dprls%
echo exit>>%dprls%
Diskpart /s %dprls% > nul 2>&1
) else (
echo Cannot find volume with label %label%
pause
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment