fkk
fkk

Reputation: 5

batch copy folder located by guid

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:

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.

Upvotes: 0

Views: 959

Answers (2)

fkk
fkk

Reputation: 5

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

Upvotes: 0

geisterfurz007
geisterfurz007

Reputation: 5874

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.

Upvotes: 0

Related Questions