-4

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
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Hi all, please keep discussion in the comments section civil (I've cleaned up the thread thus far). – josliber May 30 '18 at 01:33
  • Josliber - Understood And Thank You My Apologies! – TruckDriverOnPC May 30 '18 at 02:48
  • Compo - I Will Get The Code Added In Which I Have Had The Most Success So Far. I Appreciate The Comment And Also I Seen You Fixed My Code Pasted In Earlier Thank You For That As I Said I Am New And Just Got Enough Understanding To Paste Code Correctly. – TruckDriverOnPC May 30 '18 at 02:50
  • 4
    @TruckDriverOnPC - it's not correct English writing style to capitalize the first letter of every word. This makes what you have written more difficult to read. – Bill_Stewart May 30 '18 at 04:12
  • Can you please explain why there's a need to assign a specific drive letter to the inserted USB? I cannot see the need for forcibly assigning it to `W:` as opposed to it being auto assigned, for instance, to `F:`. The script should easily be able to set whichever drive letter is assigned to the known label without having to do all of this. Please note, I'm not saying that your step is wrong, it just seems a lot of work for something which may not actually be needed. – Compo May 30 '18 at 10:39
  • I Want The Drive Locked Onto W:\ So The Spreadsheets Can Always Search For One Another In W:\File_Destination\etc Having The Spreadsheets Always Calling For One Another Id Like To Ensure They Are Always Hard Linked. Also Ensuring The Drive Letter Assignment Will Allow The Drive To Sync To Online Backup Also. – TruckDriverOnPC May 30 '18 at 20:03

1 Answers1

0

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

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • The Only Letter To Be Assigned To This Drive Is A High Letter In This Case W:\ To Prevent User Error If They Have Many Flash Drives Etc On Their PC. I Haven't Really Considered Mounting It That Way. This Drive Will Contain Many Spreadsheets Which Call Upon Each Other During Use. That's Why I Wanted To Assign A Drive Letter. Hope That Can Make Some Sense. Also To Access The Files The Folders Are Hidden In A Maze Where The Only Sane Way To Access Them Is Run A Batch File That Will Verify The Drive Letter Of W:\ To Open Folder Or Bring Up An Error Screen. – TruckDriverOnPC May 30 '18 at 03:02