Carl's blog

How to automatically detect removable drive’s assigned letter in backup scripts.

by on Feb.24, 2011, under Computer Stuff

@echo off
if %1.==. goto noparams
goto drvid

:noparams

for %%a in (d e f g h i j k l m n o p q r s t u v w x y z) do call %0 %%a

:drvid
echo Checking if backup drive is mounted as drive %1:
if exist %1:\drvid.txt goto found
goto end

:found
Set DRVID=%1
echo Drive found as %DRVID%:

**PUT BACKUP CODE HERE. SUBSTITUTE DRIVE LETTER FOR %DRIVID%, SO E.G. USE: XCOPY C:\Windows %DRVID%:\Windows

:end


5 Comments for this entry

  • Ted

    Can you explain what the “If exist %1:\drvid.txt” is for? Does this mean it only copies the information to drives that have that file?

    I copied the script, changed the backup code, and ran it, but it still didn’t work.

    @echo off
    if %1.==. goto noparams
    goto drvid

    :noparams

    for %%a in (d e f g h i j k l m n o p q R s t u v w x y z) do call %0 %%a

    :drvid
    echo Checking if backup drive is mounted as drive %1:
    if exist %1:\drvid.txt goto found
    goto end

    :found
    Set DRVID=%1
    echo Drive found as %DRVID%:

    USE: XCOPY C:\test %DRVID%:\test

    :end

    any suggestions?

  • Ted

    Correction: Copied script to the comment wrong.

    @echo off
    if %1.==. goto noparams
    goto drvid

    :noparams

    for %%a in (d e f g h i j k l m n o p q R s t u v w x y z) do call %0 %%a

    :drvid
    echo Checking if backup drive is mounted as drive %1:
    if exist %1:\drvid.txt goto found
    goto end

    :found
    Set DRVID=%1
    echo Drive found as %DRVID%:
    XCOPY C:\test %DRVID%:\test

    :end

  • Ted

    I used a pause comment at the end to view what was happening step-by-step, and figure out the problem. How do you specify File or Directory in the script so that you dont have to manually enter it.

  • jon

    brilliant whats I was looking for to get started. great if it was complete … but thats ok i will get over it

  • Carl

    It seems I forgot to say. You have to create a file called drvid.txt and put that in the root of the removable disk.
    When the script is called without parameters, the script calls itself with a drive letter as a parameter, and checks to see if drvid.txt exists on that drive. It does this continually through each of the drive letters in the array (%%a), until drvid.txt is found on that drive. It then sets the variable %DRVID% to that letter, so you can do “backup to %DRVID%” later on in the script.

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...