Batch File Assign Drive Letter

5.5k Views Asked by At

I have found this to work and added the shutdown and restart explorer to reflect drive changes.

I cannot get it tweaked to allow search by drive letter instead of drive description.

My reason for needing to change drive letter is due to the spreadsheets inside the flash media need locked onto a specific locked directory where user error is much lower.

This is a small part of a larger batch file which checks the current drive for correct letter assignment before allowing access to the folders inside.

:Change_Letter
cls
@echo ON
setlocal
echo Type The Name Of The Drive NOT The Letter!
set /p Label=
set Drive=W
set Confirm=1
set Volume=
set VolumesFile=%Temp%\Volumes.txt
set DiskPartFile=%Temp%\DiskPart.txt
echo Retrieving volume information ...
echo list volume | diskpart.exe | more +5 | find /v "DISKPART>" >"%VolumesFile%"
for /f "tokens=2" %%a in ('type "%VolumesFile%" ^| find /i "%Label%"') do (set Volume=%%a)
if "%Volume%"=="" (
    echo No volume with the label '%Label%' found; existing volumes:
    type "%VolumesFile%"
    goto Leave
)
 >%DiskPartFile% echo select volume %Volume%
>>%DiskPartFile% echo assign letter %Drive%
>>%DiskPartFile% echo exit
if "%Confirm%" equ "0" goto AssignLetter
    echo.
    echo The following volumes were found:
    type "%VolumesFile%"
    echo 'Volume %Volume%' will be assigned the drive letter '%Drive%'.
    echo The following diskpart script will be executed:
    type "%DiskPartFile%"
    echo.
    set Response=N
    set /p "Response=Continue [y/N]? "
    if /i not "%Response%"=="Y" (
        echo Operation canceled.
        goto Leave
    )
:AssignLetter
echo Setting drive letter '%Drive%' for volume %Volume% ...
diskpart.exe /s "%DiskPartFile%"
echo Done.
:Leave
cls
CD\
Timeout /T 5
taskkill /f /im explorer.exe
Timeout /T 3
cls
CD\
start explorer.exe
if exist "%VolumesFile%" del "%VolumesFile%"
if exist "%DiskPartFile%" del "%DiskPartFile%"
Label W: LoganHayLLC
Timeout /T 4
Start W:\FileAccess.bat
exit
1

There are 1 best solutions below

1
On

The thing is that diskpart will always need admin rights to run, so it's better to avoid it if possible. Moreover if you leave the script on the flash drive you can't change the drive letter while running it anyway, because there will always an open handle on the drive so it can't be unmounted. If you force unmounting then the script will also stop running

Another problem is that you don't know if the user entered the correct drive letter. What if they entered a hard drive or another flash drive's letter?

It's better just to leave the drive letter as-is and mount the drive to another drive instead. You can use subst in a batch file instead (which can run by any users) and mount the drive that contains that batch file

@echo off
cls
echo Drive Letter Change Tool
echo.
echo Please Enter The Letter You Wish To Assign
set /P "NewLetter=Please Enter The Letter You Wish To Assign: "
if "%NewLetter:~1%"=="" if not exist %NewLetter% subst %NewLetter% %~d0

if "%NewLetter:~1%"=="" will check if the user entered only letter instead of a longer string, then that drive letter will be checked if being used

If you put the batch file in the root of the flash drive you can simply use subst %NewLetter% . instead of %~d0