1

In a batch file, can I copy a folder located by a constant path like \?\Volume{GUID}?

When copying (copy, xcopy or robocopy) a directory and its content from a local removable drive (a usb external drive for instance) to another location on the same drive, I'd like to use unique and constant absolute paths like \?\Volume{GUID} to avoid using drive letters that can change over time. To operate the copy, the batch file is meant to be placed on the removable device, but in case the file is moved or placed somewhere else I'd rather be sure it's operating on the good drive.

So far I've tried:

  • COPY can handle \?\Volume{GUID} paths to copy a file but cannot copy folders
  • XCOPY returns an "invalid drive" error
  • ROBOCOPY gives a "network path not found, wait 30 sec..."
  • for each command above : syntax variations with \?\UNC\Volume{guid} and with the trailing "\"

Am I doing something wrong or is this just not the way to do that?

Is there another way to use invariant locations?

Ideally it should involve the least tweaking possible. By tweaking I mean: labeling the drive or giving it a fixed letter, etc.

1

Naming a Volume differs from Naming Files, Paths, and Namespaces.

Next script shows possible approach how to get drive letter currently assigned to a volume. Uses Win32_Volume class:

The Win32_Volume class represents an area of storage on a hard disk. The class returns local volumes that are formatted, unformatted, mounted, or offline. A volume is formatted by using a file system, such as FAT or NTFS, and might have a drive letter assigned to it. One hard disk can have multiple volumes, and volumes can span multiple physical disks. The Win32_Volume class does not support disk drive management.

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
call :getDriveLetter "\\?\Volume{5c729d19-70f7-11e5-830d-ac220b50824f}\"
call :getDriveLetter "\\?\Volume{090c044f-7ccc-11e4-824e-806e6f6e6963}\"
call :getDriveLetter "\\?\Volume{de60a588-fbe5-11e4-826f-806e6f6e6963}\"
:endlocal
ENDLOCAL
goto :eof

:getDriveLetter
set "_volumeID=%~1"
set "_DriveLetter="
for /F "tokens=1,* delims==" %%G in ('
  wmic volume where "DeviceID='%_volumeID:\=\\%'" get DriveLetter /value
  ') do for /F %%g in ("%%~H") do set "_%%~G=%%~g"
call :doSomething
goto :eof

:doSomething
rem only output for debugging 
if defined _DriveLetter (
    echo %_volumeID% DriveLetter %_DriveLetter% 
) else (
    echo %_volumeID% DriveLetter unknown
)
goto :eof

Output:

d:\temp> D:\bat\SU\1163592.bat
\\?\Volume{5c729d19-70f7-11e5-830d-ac220b50824f}\ DriveLetter F:
\\?\Volume{090c044f-7ccc-11e4-824e-806e6f6e6963}\ DriveLetter unknown
\\?\Volume{de60a588-fbe5-11e4-826f-806e6f6e6963}\ DriveLetter E:
  • i see you took the time to reply, but personally i can't connect that to the copy process – fkk Jan 7 '17 at 10:15
  • @fkk I don't know what's unclear. For a particular USB drive, call :getDriveLetter "\\?\Volume{5c729d19-70f7-11e5-830d-ac220b50824f}\" should always return currently assigned drive letter in variable %_DriveLetter% like F: in above example. Other times returns G: (if F: letter could be occupied by another USB drive); other times returns H: (if F: and G: letters could be occupied by another USB drives) etc. etc. – JosefZ Jan 7 '17 at 17:03
  • actually just saying that none of the copy function can work with unc path would have been clearer. Thus the only way is a workaround that consists in retrieving the drive letter through wmic would have complete the answer. Here's the simplified code : for /F "usebackq tokens=1,2 delims==" %%G in (`wmic volume where "DeviceID='%_volID:\=\\%'" get DriveLetter /value`) do set _%%G=%%H . with _volID being the variable containing the unc path of the volume in question. – fkk Jan 31 '17 at 6:30
  • @fkk If this answer was helpful, please consider marking it as accepted. See this page for an explanation of why this is important. – JosefZ Jan 31 '17 at 16:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.