Question

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...

Was it helpful?

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%\"

OTHER TIPS

You could ask for the Drive Letter, like this:

Say some photos you need to back up are in directory a\b\c on our drive, whose letter changes between computers. And, you want to back your photos up to your main drive, to directory x\y\z on the main drive (drive C:)

set /p Path="Enter the drive letter for your drive on this computer. No symbols, just the capital letter"
xcopy /e /v  %Path%:\a\b\c c:\x\y\z

You could even ask for the path to back up to, like this...

set /p Path="Enter the drive letter for your removable drive on this computer. No symbols, just the capital letter."
set /p PathBackUp="Enter the path to where you want your data to be backed up on this computer. Start with a backslash."
set /p BackupDrive="Enter the drive letter for your data to be backed up to on this    computer. No symbols, just the capital letter."
xcopy /e /v  %Path%:\a\b\c %BackupDrive%:%PathBackUp%

Or Both...

set /p Path="Enter the drive letter for your removable drive on this computer. No symbols, just the capital letter."
set /p DataPath="Enter the path where your data to be backed up is. Start with a backslash, and don't include the drive letter.
set /p PathBackUp="Enter the path to where you want your data to be backed up on this computer. Start with a backslash, and don't include the drive letter."
set /p BackupDrive="Enter the drive letter for your data to be backed up to on this    computer. No symbols, just the capital letter."
xcopy /e /v  %Path%:%DataPath% %BackupDrive%:%PathBackUp%

I guess this kind of defeats the purpose of automation, but just a thought; maybe this will help.

For more info about the xcopy command and modifiers, see http://www.computerhope.com/xcopyhlp.htm

For more info about the set command, see http://www.computerhope.com/sethlp.htm

This will simply find all USB drives connected to the system.

@echo off

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (

if %%l equ 2 (
echo %%i is a USB drive.
        )
        )

I was searching everywhere and trying many things - this is the fastest and simplest drive letter switch command line option

echo off

set /p Path="Enter the Drive Letter of the source File then press enter___"

set /p Pathde="Enter the Drive Letter of the Destination File then press enter___"

echo on

COPY %path%:\Users\Dell\Desktop\test.txt %pathde%:\test.txt

I wrote a small C programm (lpath.exe) to address the problem of changing drive letters of removable devices.

The idea is to create a directory (that can be hidden) on the related USB device or use the drive/disk name set using Windows explorer.

The lpath program checks all available disks if the directory or disk name exists and returns the current drive letter to be used in the batch file:

Create the directory on USB disk:

mkdir f:\USB-DISK1

To resolve drive letter:

lpath USB-DISK1:\myImages\House

this returns g:\myImages\House when the USB disk is currently assigned to the drive letter g:

In a script use for example:

setlocal EnableDelayedExpansion
set input=USB-DISK1:\exports\weekly
for /f "tokens=*" %%r in ('echo !input!^| lpath') do set input=%%r
echo resolved directory is: !input!

A more convenient way is to create a lset.cmd alongside the lpath.exe program

@echo off
rem
rem lset.cmd - set variable with resolved label path
rem
rem [00] 01.05.2019 CWa Initial Version
rem [02] 15.06.2019 CWa ++
rem
if not {%COMPUTERNAME%}=={!COMPUTERNAME!} echo %~n0-ERROR: use this function with 'setlocal EnableDelayedExpansion' only & exit /b 7

rem USAGE: lib\lset variable "path"    - set variable with resolved label path

set __lset_path=%~2
if {!__lset_path!}=={} set %1=& exit /b
for /f "tokens=*" %%r in ('echo !__lset_path!^| "%~dp0lpath"') do set %1=%%r
set __lset_path=

and use it in a main batch script, as:

setlocal EnableDelayedExpansion
call lset input USB-DISK1:\exports\weekly
echo resolved directory is: !input!

For a more detailed manual, see: lpath - expand a path containing labels

A compiled version of lpath.exe and the lset.cmd can be found in WA2LWinTools\lib of the WA2L/WinTools package.

The disadvantage of this solution is, that you need at least one additional file to distribute alongside your script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top