×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Dynamic assignment of drive letters

Dynamic assignment of drive letters

Dynamic assignment of drive letters

(OP)
I have an external hard drive attached to a machine running Windows 7. The drive is mains-powered, with its own on/off switch, and is connected to the PC via USB. I only use the drive for my daily end-of-day backup. Most of the time it is switched off. This is partly to save wear and tear on the drive, but mainly because of the noise. I switch it on just before I do the backup, and switch it off when the backup is finished.

Basically, this works OK. Windows sees the drive as Drive G:. My backup software is configured accordingly.

However, if, during the course of the day, I attach some other device that Windows considers to be an external drive (a thumb drive, my Kindle, or similar), then that device is given the letter G:. And when I later switch on the backup drive, it now becomes Drive H: This is true even if I eject the other drive before switching on the backup drive, and even if I reboot the machine.

Furthermore, if I later insert yet another device, that now becomes Drive H: and the backup drive becomes Drive I: - and so on up the alphabet.

This is a bit of a nuisance as it means going into my backup utility and changing the destination drive letter each time. It's not that difficult, but it takes time, right at the end of the day when I want to finish work.

Another solution is to go into Disk Management and re-assign the drive letter there. But, again, it's a nuisance to have to do that. Or, every time I want to insert a disk-type device, I could try to remember to switch the backup drive on first, and to switch it off again after I have ejected the other device. I find it hard to always remember to do that.

I was just wondering if anybody has a better solution. Ideally, something that doesn't require too much thinking on my part.

Thanks in advance.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Dynamic assignment of drive letters

Try starting from the back end. You could use the subst command

subst Z: <letter of backup device>

Then always run your backup to Z:. If you haven't done the subst, Z: won't exist and your backup will fail immediately.

One of the problems is that USB, net use and subst don't always know about each other. If you set a network share using

net use E: ...

and E: is the next available device, on some versions of windows it would never see the USB device until you removed the network share. Same with subst. The normal workaround is to start from the middle (P onwards) or from the end (Z backwards) and not to use any letters at the front end.

RE: Dynamic assignment of drive letters

Many years ago I put together a "batch file" to get around this sort of problem, getting a lot of help from Prof Google along the way.  It is presented below for your possible use.  I still use it from time to time (now on a Windows-10 computer), but have long forgotten what the various arcane commands achieve.  So beyond offering you the code I will be unable to provide any help.

CODE

@Echo Off
  REM
  REM  BATCH FILE TO ATTEMPT TO ENSURE THAT A PORTABLE VOLUME WITH A GIVEN NAME
  REM  IS RE-ASSIGNED A GIVEN DRIVE LETTER.
  REM
  REM  It will probably not work for a volume whose name contains embedded blanks.
  REM
  REM  The batch file seems to need to be RUN AS ADMINISTRATOR.
  REM  One suggested way to make this easier is to create a shortcut to it, and set
  REM  the shortcut to be run "as administrator":
  REM     (1) Create a shortcut to the batch file.
  REM     (2) Right click the shortcut, click Properties.
  REM     (3) On the Shortcut tab, click the Advanced button.
  REM     (4) Check the box for Run as Administrator.
  REM
ECHO.
ECHO This batch file needs to be run as Administrator.
ECHO.
PAUSE
  REM
SetLocal
  REM
  REM  SET A DEBUG SWITCH.
  REM
  REM  If and only if this has the value "Yes", the batch file will pause
  REM  after echoing certain progress messages, and the temporary working files
  REM  will NOT be deleted from the %TEMP% directory at completion.
  REM
Set tDebug=No
  REM
  REM  GET THE VOLUME NAME AND THE DESIRED DRIVE LETTER.
  REM
  REM  Trap a null name or a null drive letter.
  REM  Then convert valid results to upper case.
  REM
Set vName=%1
If _%1==_ set /p vName=Enter the NAME of the volume requiring a specific drive letter: 
If _%vName%==_ (Echo No volume name has been entered. & Goto :TheEnd)
Call :ToUpper vName
  REM
Set vLett=%2
If _%2==_ set /p vLett=Enter the wanted DRIVE letter (without the colon): 
If _%vLett%==_ (Echo No drive letter has been entered. & Goto :TheEnd)
Call :ToUpper vLett
  REM
  REM  FIND THE DRIVE NUMBER CURRENTLY ASSIGNED TO THE VOLUME.
  REM
  REM  While we are at it, also get the currently assigned drive letter.
  REM  If this letter is already the one we want, the job is over before it began.
  REM  The use of spaces around FIND's target avoids getting a partial match.
  REM
For /f "tokens=2,3" %%a in ('echo list volume ^| diskpart ^| find /i " %vName% "') do (
  set vNumb=%%a
  set tLett=%%b
  )
Echo The DISKPART program tells us %vName% is drive %vNumb% and has letter %tLett%.
If _%tDebug%==_Yes Pause
If _%vNumb%==_ (Echo Volume %vName% is nowhere to be seen. & GoTo :TheEnd)
If _%tLett%==_%vLett% (
  Echo Volume %vName% already has drive letter %vLett% assigned to it.
  GoTo :TheEnd
  )
Echo Volume number for %vName% is %vNumb%.  Its initial drive letter is %tLett%
Echo     whereas %vLett% is the drive letter that has been requested.
  REM
  REM  CHECK WHETHER THE REQUESTED DRIVE LETTER IS ALREADY ASSIGNED TO ANOTHER VOLUME.
  REM
Set tCommand='Vol %vLett%:'
Echo About to issue the command  %tCommand%  to get the name associated with drive %vLett%:
If _%tDebug%==_Yes Pause
For /f "tokens=6" %%t in (%tCommand%) do @set tName=%%t
If  NOT _%tName%==_ Call :ToUpper tName
Echo The VOL command tells us drive %vLett%: has label %tName%.
If _%tDebug%==_Yes Pause
If _%tName%==_        (Echo Drive %vLett%: is available for use. & Goto :DoAssign)
If _%tName%==_NO      (Echo Drive %vLett%: is already in use by an unnamed volume. & Goto :TheEnd)
If _%tName%==_%vName% (Echo As reported above, the assignment is already correct. & Goto :TheEnd)
Echo Drive %vLett%: is already in use by another volume, %tName%.
Goto :TheEnd
  REM
  REM  ASSIGN THE REQUESTED LETTER TO THE REQUESTED VOLUME.
  REM
:DoAssign
Echo We are about to assign drive letter %vLett% to the volume named %vName%.
Set InputFileName=%Temp%\T$_ForDiskpart.txt
Set OutputFileName=%Temp%\T$_FromDiskpart.txt
Echo REM File containing scripted commands for the DISKPART program. > %InputFileName%
Echo SELECT VOLUME=%vNumb%                  >>                         %InputFileName%
Echo ASSIGN LETTER=%vLett%                  >>                         %InputFileName%
Echo EXIT                                   >>                         %InputFileName%
Echo.
Echo We have created a DISKPART script file as follows:
Type %InputFileName%
Echo.
If _%tDebug%==_Yes Pause
  REM  Now just do it.
DISKPART /s %InputFileName%  >  %OutputFileName%
Echo.
Echo The output from the DISKPART program was as follows:
Type %OutputFileName%
Echo.
If _%tDebug%==_Yes Pause
  REM
  REM  A BIT OF CLEANING UP.
  REM
  REM  No need to purge the environment variables created, because of the
  REM  use of the SetLocal and EndLocal commands.
  REM
If NOT _%tDebug%==_Yes (DEL %InputFileName% & DEL %OutputFileName%)
  REM
  REM  THAT'S ALL FOLKS.
  REM
:TheEnd
EndLocal
Pause
Exit
  REM
  REM  S U B R O U T I N E S
  REM
:ToUpper
:: Subroutine to convert a variable VALUE to all UPPER CASE.
:: The argument for this subroutine is the variable NAME.
For %%i in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
            "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
            "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z")      Do Call set "%1=%%%1:%%~i%%"
Exit /b 

RE: Dynamic assignment of drive letters

Quote (Mike Lewis)

Another solution is to go into Disk Management and re-assign the drive letter there. But, again, it's a nuisance to have to do that. Or, every time I want to insert a disk-type device, I could try to remember to switch the backup drive on first, and to switch it off again after I have ejected the other device. I find it hard to always remember to do that.

Have you tried this? This is what I do and I've never had to set the drive letter again. It seems to be persistent, locked to the volume ID.

It's also wise, as noted by xwb, to choose a letter that's deeper into the alphabet.

RE: Dynamic assignment of drive letters

> go into Disk Management and re-assign the drive letter there
> It seems to be persistent

It is supposed to be. But only if the drive letter is free (i.e. it is not an exclusive reservation). What I normally do is to use Disk Management to assign the drive letter, but to start (as spamjim and xwb have said) at the end of the alphabet and work backwards

RE: Dynamic assignment of drive letters

(OP)
Thank you all for your very helpful replies.

I'm going to start by trying XWB's suggestion of assigning Z to the backup drive letter. That's a nice simple solution. I expect that it will solve the problem, but I'll try it for a day or two to be sure.

Another possibility is to see if I can configure the backup program to reference a volume name or serial number rather than the drive letter. Unfortunately, the program's docs are a little obscure, but I will work on it.

Deniall, thanks for posting the batch file. It was interesting to read it, not least to remind myself of the quirks of the batch language, but with luck one of the above solutions should prove adequate.

In any case, I'll report back once I've decided what to do.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Dynamic assignment of drive letters

(OP)
Just to wrap up this thread ....

I used Disk Management to assign the back-up drive as Z:, and have configured the backup utility accordingly. The assignment seems to be persistent. No matter what other devices I connect or disconnect during the day, the backup drive is always Z.

So thanks again for all your help. This is a nice simple solution, and I feel a bit of a clot for not thinking of it myself.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close