Carl's geek blog
How to automatically detect removable drive’s assigned letter in backup scripts.
@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
| Print article | This entry was posted by Carl on February 24, 2011 at 12:15, and is filed under Computer Stuff. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
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?
about 1 year ago
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
about 1 year ago
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.
about 1 year ago
brilliant whats I was looking for to get started. great if it was complete … but thats ok i will get over it
about 1 year ago
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.