Search code examples
batch-fileusb-drivedrive-letter

Batch file and variable USB drive letters


I'm trying to create a backup batch file to automate Acronis True Image and backup maintenance.

The USB drive letter changes depending on which machine I'm backing up and /or how many devices are connected and using drive letters...

My batch resides in C:\ My batch will call ATI. ATI dumps an image onto C:\ The batch then looks for my external based on its volume serial number like so:

SET SN=30BC-F5A4
SET found=

0>NUL SET /P=Searching for external drive...
TIMEOUT 3 /NOBREAK >NUL

FOR %%N IN (D E F G H I J K L M O P Q R S T U V W X Y Z) DO (
IF defined found CALL :MOVE
VOL %%N: 2>NUL | FIND "%sn%" >NUL && CALL :MOVE %%N
)

ECHO  Fail^^!
TIMEOUT 3 /NOBREAK >NUL

ECHO.
ECHO Connect it now and cancel "Autoplay".
TIMEOUT 3 /NOBREAK >NUL

ECHO.
ECHO Press any key to try again...
PAUSE >NUL

CALL: EXTERNAL



:: ***MOVE BACKUP***

:MOVE

OK, that work just fine, but what I'm needing now is something along the lines of;

Find the correct external device based off of serial number. (As above) Grab the correct drive letter and turn it into a variable. Break out of the FOR IN DO loop when the drive letter is found and make it a variable. Use variable to check if a folder exists. If the folder doesn't exist, create it. If the folder does exist, check if MyBackup.tib exists within. If so, delete the file. CHDIR or somesuch back to where the batch resides, (C:\) and MOVE the newly created backup into the folder on the external.

I'm coming up on ten hours straight working on this issue and I'm no closer to finding the correct syntax/command(s) than I was at the start.

PLEASE, can you help!? I'll give you a cookie, lol!

I'm running Windows 7 Ultimate if that helps...


Solution

  • The serial number is a number in hexadecimal notation (with a dash thrown in much like commas are used as a thousands separator in decimal numbers).

    WMIC volume get driveLetter, serialNumber can be used to get a listing of all drive letters with their serial number in decimal notation. Your serial number of 30BC-F5A4 equates to decimal 817690020. I converted your hex SN into decimal form using the following command on the command line: set /a 0x30BCF5A4.

    So the following command will set a variable to the drive letter (with colon) of your external drive.

    set SN=817690020
    set "extDrive="
    for /f %%D in ('wmic volume get driveLetter^, serialNumber ^| find "%sn%"') do set extDrive=%%D
    if not defined extDrive echo Error finding external drive & exit /b
    

    I don't understand where the folder name comes from that you talk about. I'm assuming you have a variable with the value defined somehow. I'll simply name the variable "folder".

    set folder=someFolderName
    if not exist "%extDrive%\%folder%\" mkdir "%extDrive%\%folder%"
    

    You can use the MOVE command to move your newly created backup to the folder on the external drive, automatically overwriting any existing backup if it already exists.

    move /y "c:\MyBackup.tib" "%extDrive%\%folder%\"