Limit range of drive letters that can be assigned to USB drives?

Page 1 of 2 12 LastLast

  1. Posts : 1,750
    Windows 10 Pro
       #1

    Limit range of drive letters that can be assigned to USB drives?


    I have these scripts that I use with some specific external drives, let's say I: and J:. the scripts require that the external drive always have the same letter.

    The issue is that if another USB drive was plugged in first, and Windows assigned either I: or J: to this other drive. So when I plug in my specific external drive, it gets a different drive letter and I have to move drive letters around with Administrative tools, Disk Management, before I can run my scripts.

    Any way to tell Windows to never give out some drive letters to a random USB drive?
      My Computers


  2. Posts : 7,871
    Windows 11 Pro 64 bit
       #2

    Good question!
      My Computers


  3. Posts : 16,786
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #3

    I adopted a different approach for the scripts I have for using specific external drives. I have a batch file, used as a subroutine for others, that accepts a passed argument of drive label and returns the drive letter [it also works the other way around].

    I have adjusted this batch file to cope with the case of drives with no label. Its use remains the same - see FindDriveLetter-Label - TenForums.bat [post #36] - TenForums

    I set* a meaningful drive label for my backup drives [in your case, your drives I J] and then use my batch file to get the drive letter currently being used by the drive that has that label.
    * When I first set up a new drive after purchase, I check that the label has 'taken' by looking at the drive Properties not just at how it is displayed in File explorer because I have, quite surprisingly, found differences recently.

    So, my script controlling a backup has these lines when retrieving the drive letter of a drive labelled Fred
    Code:
    Set /a FredValue=0
    Set ThisTest=Fred
    Call C:\Tools\IdentifyDrives.bat %ThisTest%
    :: If result is a two letter string [a successful result is a letter and a colon], Fred is connected
    If "%DriveResult:~2%"=="" (Set /a FredValue=1) & (Call :GetOnWithIt)
    GoTo EndFredBackup
    - The variable %DriveResult% identifies which drive letter the drive labelled Fred currently has.
    - My variable %FredValue% is used in decision points later in the script {e.g. If %FredValue% EQU 0 GoTo EndFredBackup}
    - My subroutine :GetOnWithIt is the section of code that runs the backup
    {All my scripts are within subfolders of C:\Tools as explained in my post Set up my Tools folder ditty - TenForums}

    The batch file doing the assessment is
    IdentifyDrives.bat
    Code:
    :::::::::: Pass drive label or drive letter when calling this script and get back drive letter or drive label ::::::::::
    :::::::::: - i.e. pass one, get the other ::::::::::
    :::::::::: - The output is the variable DriveResult ::::::::::
    :: Initialisation
    Set DriveResult=
    
    :: Assess passed argument
    :: Remove any quotes
    Set ThisPassed=%~1
    :: Bail out if no parameter had been passed
    If "FFFFFFFF"=="FFFF%ThisPassed%FFFF" GoTo EndIDDrives
    :: Remove any colon
    Set ThisPassed=%ThisPassed::=%
    :: If ThisPassed is a single letter it must be a drive letter and I must want to get the label
    If "%ThisPassed:~1%"=="" GoTo GetDriveLabel
    :: Otherwise ThisPassed must be a label and I must want to get the drive letter
    GoTo GetDriveLetter
    
    :: Passed drive label, get drive letter [including colon]
    :GetDriveLetter
    for /f "tokens=1,2" %%D in ('wmic volume get DriveLetter^, Label') do Call :CheckLabels %%D %%E
    If "%DriveResult%"=="" (Set DriveResult=PassedParameterNotFound) & GoTo EndIDDrives
    GoTo EndIDDrives
    
    :CheckLabels
    :: Further processing required to avoid labels being misidentified
    :: - FredBig, Fred, FredSmall would otherwise all get a hit on finding Fred
    :: Check that the found label is an exact match for ThisPassed
    If /I "%2"=="%ThisPassed%" Set DriveResult=%1
    GoTo :EOF
    
    :: Passed drive letter [including colon], get drive label
    :GetDriveLabel
    for /f "tokens=2"  %%D in ('wmic volume get DriveLetter^, Label ^| find /I "%ThisPassed%:"') do Set DriveResult=%%D
    If "%DriveResult%"=="" (Set DriveResult=PassedParameterNotFound) & GoTo EndIDDrives
    GoTo EndIDDrives
    
    :EndIDDrives
    Pause at EndIDDrives during testing - DriveResult is %DriveResult%

    The code can also cope with passing a drive letter and getting back a drive label but I have yet to find a single example of that being useful.

    Denis
    Last edited by Try3; 15 Aug 2021 at 08:24.
      My Computer


  4. Posts : 16,786
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    I don't understand the colour coding scheme used in TenForums code boxes so here's an imperfect alternative display of the code I posted above.
    Limit range of drive letters that can be assigned to USB drives?-shown-word.png

    Colour codes:
    blue - comments
    mauve - variable processing
    brick red - decision processing
    green - program flow
    light blue - brackets
    orange - external commands


    Denis
    Last edited by Try3; 10 Sep 2019 at 06:26.
      My Computer


  5. Posts : 17,661
    Windows 10 Pro
       #5

    When I customize Windows deployment images, I copy Windows ISO contents to a folder D:\ISO_Files. I can then mount the image, edit it, add answer files and customizations and so on.

    When done, I use this simple batch file to copy the customized install files to a USB Flash drive. It looks for an USB Flash drive with label W10-USB, gets its drive letter, formats it giving it the same drive label (W10-USB), and finally copies the contents of folder D:\ISO_Files to that USB drive:

    Code:
    @ECHO OFF
    for /f %%X in ('wmic volume get driveletter^, label ^| find "W10-USB"') do set USBDRIVE=%%X
    format %USBDRIVE% /fs:NTFS /v:W10-USB /q
    echo.
    echo Copying D:\ISO_Files to drive %USBDRIVE%
    xcopy D:\ISO_Files\*.* /s /e /q %USBDRIVE% /i /k
    echo .
    echo D:\ISO_Files copied to USB drive %USBDRIVE%
    echo.
    pause

    I have the batch file saved as CopyToUSB.bat in my %userprofile%\OneDrive\Scripts folder. The batch reads USB drive W10-USB's letter to variable %USBDRIVE%, and uses it in format and xcopy commands. As long as I use the same USB flash drive label (W10USB), I don't have to know its drive letter.

    Limit range of drive letters that can be assigned to USB drives?-image.png

    Kari
      My Computer


  6. Posts : 1,750
    Windows 10 Pro
    Thread Starter
       #6

    Kari said:
    When I customize Windows deployment images, I copy Windows ISO contents to a folder D:\ISO_Files. I can then mount the image, edit it, add answer files and customizations and so on.

    When done, I use this simple batch file to copy the customized install files to a USB Flash drive. It looks for an USB Flash drive with label W10-USB, gets its drive letter, formats it giving it the same drive label (W10-USB), and finally copies the contents of folder D:\ISO_Files to that USB drive:

    Code:
    @ECHO OFF
    for /f %%X in ('wmic volume get driveletter^, label ^| find "W10-USB"') do set USBDRIVE=%%X
    format %USBDRIVE% /fs:NTFS /v:W10-USB /q
    echo.
    echo Copying D:\ISO_Files to drive %USBDRIVE%
    xcopy D:\ISO_Files\*.* /s /e /q %USBDRIVE% /i /k
    echo .
    echo D:\ISO_Files copied to USB drive %USBDRIVE%
    echo.
    pause

    I have the batch file saved as CopyToUSB.bat in my %userprofile%\OneDrive\Scripts folder. The batch reads USB drive W10-USB's letter to variable %USBDRIVE%, and uses it in format and xcopy commands. As long as I use the same USB flash drive label (W10USB), I don't have to know its drive letter.

    Limit range of drive letters that can be assigned to USB drives?-image.png

    Kari
    Guys,

    I'm amazed at the ingenuity in these scripts, but most of my scripts are actually "jobs" in programs like GoodSync and Retrospect. They only work if they find the external drive at the drive letter built into those "jobs." Retrospect in particular, it is very, very particular about drive IDs and drive letters.

    I have made unforced user errors ( ) because I plugged in a USB drive and then did some work, without first checking the drive letter that Windows assigned. Those user errors would eliminated if I could map "other" USB drives away from the drive letters I want to reserve.

    - - - Updated - - -

    Kari said:
    When I customize Windows deployment images, I copy Windows ISO contents to a folder D:\ISO_Files. I can then mount the image, edit it, add answer files and customizations and so on.

    When done, I use this simple batch file to copy the customized install files to a USB Flash drive. It looks for an USB Flash drive with label W10-USB, gets its drive letter, formats it giving it the same drive label (W10-USB), and finally copies the contents of folder D:\ISO_Files to that USB drive:

    Code:
    @ECHO OFF
    for /f %%X in ('wmic volume get driveletter^, label ^| find "W10-USB"') do set USBDRIVE=%%X
    format %USBDRIVE% /fs:NTFS /v:W10-USB /q
    echo.
    echo Copying D:\ISO_Files to drive %USBDRIVE%
    xcopy D:\ISO_Files\*.* /s /e /q %USBDRIVE% /i /k
    echo .
    echo D:\ISO_Files copied to USB drive %USBDRIVE%
    echo.
    pause

    I have the batch file saved as CopyToUSB.bat in my %userprofile%\OneDrive\Scripts folder. The batch reads USB drive W10-USB's letter to variable %USBDRIVE%, and uses it in format and xcopy commands. As long as I use the same USB flash drive label (W10USB), I don't have to know its drive letter.

    Limit range of drive letters that can be assigned to USB drives?-image.png

    Kari
    Guys,

    I'm amazed at the ingenuity in these scripts, but most of my scripts are actually "jobs" in programs like GoodSync, Beyond Compare, and Retrospect. They only work if they find the external drive at the exact drive letter built into those "jobs."

    I have made unforced user errors ( ) because I plugged in a USB drive and then did some work, without first checking the drive letter that Windows assigned. Those user errors would eliminated if I could map "other" USB drives away from the drive letters I want to reserve.
      My Computers


  7. Posts : 16,786
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #7

    Then it seems to me that your only option is to assign drive letters that will not get used by anything else - assign YZ instead of IJ.

    Denis
      My Computer


  8. Posts : 1,750
    Windows 10 Pro
    Thread Starter
       #8

    Try3 said:
    Then it seems to me that your only option is to assign drive letters that will not get used by anything else - assign YZ instead of IJ.

    Denis
    I was hoping that would not be the answer, but what is, is. Even with this approach, I have 4 different external USB drives that are part of these scripts, so I would still have the issue of matching the drive to a specific letter.

    I wish there was some way to submit a feature request to Microsoft.
      My Computers


  9. Posts : 16,786
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #9

    If you assign them [once only] to WXYZ using Disk mgmt {C:\Windows\System32\diskmgmt.msc} or something like MiniTool Partition Wizard then they will retain those drive letters when they are re-connected unless
    - you first connect 19+ other drives at the same time as your desired drives so that the computer is forced to re-use W then X then Y then Z
    - you do a Windows 10 Version update - drive assignments do need to be checked afterwards [but this has only messed up drive assignments a couple of times].
    - [added 14 Sep 2019] - some Cumulative updates can mess up drive letter assignments as well. KB4515384 does so on one of my computers [but not on the others].

    You could use the Feedback hub to suggest that change but, as explained, I think Windows can already achieve what you want without any change being necessary.

    Denis
    Last edited by Try3; 14 Sep 2019 at 04:49.
      My Computer


  10. Posts : 1,750
    Windows 10 Pro
    Thread Starter
       #10

    Denis,

    Thanks. After re-reading your previous message, I think that I can mark this thread SOLVED.

    Appreciated.
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 10:39.
Find Us




Windows 10 Forums