batch copy folder located by guid

929 views Asked by At

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.

2

There are 2 answers

0
fkk On BEST ANSWER

None of the copy function can work with unc path. Thus the only way is a workaround that consists in retrieving the drive letter through wmic command. Here's the 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

2
geisterfurz007 On

Have a look at pushd /? and popd /?

pushd \\?\UNC\Volume{guid}

will short time map the UNC path as a network drive and change the directory to it according to the next free drive of your computer from the end of the alphabet.

For example, you got Z:\ and Y:\ already it will map the path to X:\ and change the current directory to X:\.

From that on you can go for copy filefromServer.foo C:\localfile.bar

The other commands such as XCopy can use the mapped drive as well.

To unmap the drive use popd.