Search code examples
batch-filexcopy

Bat file to copy to device based on device name not drive letter


Not sure the best way to accomplish this and looking for a point in the right direction.

Currently use a Bat file to fetch Backup files and copy them to an external device. For example:

XCOPY C:\MainFolder\Backups\*.* G:\Backupfolder\ /D /S /E /Y

I am trying to encourage users to backup to more than one device and to keep one device offsite. The backup usually occurs overnight and is setup as a scheduled task. Most of the users are computer illiterate.

As we know, you can plug in multiple USB devices and it will assign it a new drive letter. Is there any way i can Copy to a device based on its name opposed to drive letter? Its too complicated for users to go into Computer management and assign it the correct drive letter.

The only thing i can think of, is using Bat file to copy to a array of Drive letters, but with this i can see it backing up to things that it should not.


Solution

  • Assuming you know the Volume name of the removable disk:

    @echo off
    for /f %%i in ('wmic logicaldisk where "drivetype=2" get Name^, VolumeName ^| findstr /i "Name Of USB volume here"') do echo %%i
    

    The above will echo the drive letter of the volume name.

    Where the below will echo both Volumename and Drive letter:

    @echo off
    for /f "tokens=1,*" %%i in ('wmic logicaldisk where "drivetype=2" get Name^, VolumeName ^| findstr /i "Name Of USB volume here"') do echo %%i %%j
    

    You can run wmic on its own to see other results:

    wmic logicaldisk get Name, VolumeName, Caption, DriveType
    

    As a side note. Removable drive types are 2