In a batch, how to specify a path that use a volume name instead of a drive letter?

Posted on

QUESTION :

In a batch, is there a way to specify a path using the volume name instead of a drive letter ? (eg: something like \RECOVERYDocs…)

Or, if not possible, is there a way to check if a specific volume name belongs to a specific drive letter (eg : if F: is RECOVERY do this else do that)

Here is what i want to do : to create a batch that backup one drive to another, but since both drives (source and destination) are on removable usb drives I cannot be sure that drive letters will never change. Copying backup to working folder instead of opposite would be a catastrophic failure.

ANSWER :

Personally I would use WMI (wmic if cmd is a must) and get label -> drive letter link from there. It’s not very hard to do, but I kind of agree with a comments that batch file may not be the best solution… for so fragile backup operations. That said, here logic to get there quickly:

@echo off
set VAR=Source
for /f "skip=1" %%L in ('wmic logicaldisk where volumename^="SourceLabel" Get Caption') do @call :SetVar %%L
set VAR=Destination
for /f "skip=1" %%L in ('wmic logicaldisk where volumename^="DestinationLabel" Get Caption') do @call :SetVar %%L

echo Copying data from %Source% to %Destination%
goto :EOF

:SetVar
set Label=%1
if NOT [%Label%]==[] set %VAR%=%Label%
goto :EOF

All you need to do is replace SourceLabel with real label of source drive, same with destination label. Once you get correct references in echo command – you can put any other logic between it and “goto :EOF” directive.

Leave a Reply

Your email address will not be published.