1

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

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.

7
  • I'm not sure writing a backup program in batch is a good idea...
    – Daniel Beck
    Jul 12, 2012 at 19:13
  • in fact, I will use a command line tool that will do the job for me. eg: sync "D:\documents" "E:\backup"
    – tigrou
    Jul 12, 2012 at 19:14
  • If your program can handle UNC paths, using \\?\Volume{GUID} would be a good idea, see e.g. here how to find out these paths (once).
    – Daniel Beck
    Jul 12, 2012 at 19:16
  • To find out volume UNC paths, run mountvol C: /L in cmd, C: being the drive letter. I'll post this as an answer if you confirm it works. If it doesn't you can always mount these volumes to specific drive letters further back in the alphabet (X for source, Y for destination), and use these afterwards...
    – Daniel Beck
    Jul 12, 2012 at 19:26
  • I already figured out how to find guid using HKLM\System\MountedDevices, however mountvol is faster :D. The command line tool is written in C#. I tried to give him UNC paths but it throw an exception of type System.Security.Permissions.FileIOPermission.HasIllegalCharacters.
    – tigrou
    Jul 12, 2012 at 19:31

1 Answer 1

2

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.

2
  • great! one last question : how can i easily check if one of the volumenames is not found and print a message ?
    – tigrou
    Jul 12, 2012 at 22:37
  • You just need to test for var being defined, e.g. if not defined Source (echo Source not found&&goto :EOF) if not defined Destination (echo Destination not found&&goto :EOF)
    – BartekB
    Jul 12, 2012 at 23:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .