如何在Windows路径中使用卷标? [英] How to use a volume label in a Windows path?

查看:266
本文介绍了如何在Windows路径中使用卷标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用批处理文件从可移动驱动器中复制文件,而不管它得到的驱动器号是什么.

I want to copy files off a removable drive using a batch file, regardless of the drive letter it gets.

到目前为止,不行.似乎没有任何可用的命令或第三方命令行工具可以根据卷标处理路径.

So far, no go. There don't seem to be any readily available commands or 3rd-party command-line tools that would handle paths based on volume labels.

我尝试了FreeFileSync,但是它可以大量工作,并且在这里我需要精确的文件操作.另外,它不会删除文件,我需要从笔式驱动器上移走文件.

I tried FreeFileSync, but it works in big batches, and I need precise file operations here. Also, it doesn't do deletions, and I need to MOVE files off the pendrive.

但是,引起我兴趣的是发出像...这样的废话命令.

What piques my interest, however, is that issuing a nonsense command like...

C:\> cd MYPENDRIVE:

...在默认错误消息中产生了非常有趣的一点:

... results in quite an interesting bit in the default error message:

The filename, directory name, or volume label syntax is incorrect.
                                 ^^^^^^^^^^^^^^^^^^^

如果此消息是可信任的,则意味着存在一些用于在其中放置卷标的正确语法.还是不在那里?

If this message is to be trusted, then it means there IS some correct syntax for putting a volume label in there. Or isn't there?

注意:如果需要的话,我可以写一个小的"getdriveletterforvolume"工具,但是我不想重蹈覆辙.另外,在较长的批处理文件操作期间,该驱动器可能会被卸下并替换为另一个驱动器,此时,我将需要停止该操作,而不是在另一个获得相同驱动器号的驱动器上继续操作.

Note: I can settle for writing a small "getdriveletterforvolume" tool, if I have to, but I'd rather not reinvent the wheel. Also, during longer batch file operations the drive may be removed and replaced with another, at which point I'll need the operations to cease, and not continue on another drive that gets the same drive letter.

注2:使用\\?\ Volume {12345678 .....}是最后的选择,尽管可以在一定程度上加以驯服,以构建基本的批处理文件,例如...

Note2: Using \\?\Volume{12345678.....} is a last resort, though it can be tamed to some extent, to build a rudimentary batch file like...

SET PENDRIVE=\\?\Volume{949a764e-a2f0-11e3-b710-6cf04975450b}
SET BACKUPDRIVE=\\?\Volume{e79091c4-5c2a-11e3-86f9-806e6f6e6963}

if exist %PENDRIVE% {
  if exist %BACKUPDRIVE% {
    move %PENDRIVE%\*.* %BACKUPDRIVE%
  }
}

...但是这很丑陋,并且不允许我做任何魔术,就像给两个Pendrive赋予相同的标签以使其表现相同一样.我知道这不是常见的情况,但是,嘿,为什么要在可能有现成的解决方案的地方限制自己的选择?

... but it's ugly and doesn't let me do any magic like giving two pendrives the same label to have them behave identically. I know it's not a common case, but hey, why limit one's options where there might be a ready solution?

进度.我修改了子例程以将值返回为VOLUMEID:

Progress. I modified the subroutine to return a value as VOLUMEID:

CALL :GetVolumeID PENDRIVE
SET "PENDRIVE=%VOLUMEID%"
IF EXIST "%PENDRIVE%\" ECHO PENDRIVE is connected at %PENDRIVE%!

GOTO :eof

:GetVolumeID
FOR /F "skip=1" %%A in ('"wmic volume where label='%~1' get deviceid 2>/null"') do (
 SET "VOLUMEID=%%A"
 GOTO :eof
)
GOTO :eof

...但是现在,如果缺少PenDrive,wmic将返回一些类似空的值(不是空字符串;它不会通过if %%A==""检查)-因此IF EXIST \最终为true.如何消除这样的空结果?我在FOR之前尝试了SET VOLUMEID=$FOO$,但是无论如何它都会用一个空值覆盖它.

... but now if the pendrive is missing, wmic returns some empty-like value (NOT an empty string; it fails an if %%A=="" check) - and so IF EXIST \ ends up true. How do I eliminate an empty result like that..? I tried SET VOLUMEID=$FOO$ before the FOR, but it overwrites that with an empty value anyway.

最后!这是一个概念证明,对于任何认为有用的人.

Finally! Here's a proof of concept, for whomever finds it useful.

@ECHO OFF

CALL :GetVolumeID BACKUPDRIVE
SET "BACKUPDRIVE=%VOLUMEID%"

CALL :GetVolumeID PENDRIVE
SET "PENDRIVE=%VOLUMEID%"

if exist %PENDRIVE%\ (
    if exist %BACKUPDRIVE%\ (

        echo Emptying the pendrive.

        move %PENDRIVE%\*.* %BACKUPDRIVE%\pendrive\
    )
)

GOTO :eof

:GetVolumeID
SET VOLUMEID=$$$
FOR /F "tokens=1,* delims==" %%A IN ('"wmic volume where label='%~1' get deviceid /value 2>nul"') DO  IF NOT "%%~B"=="" SET "VOLUMEID=%%~B"
SET "VOLUMEID=%VOLUMEID:~0,-2%"
GOTO :eof

我添加了$虚假值,以使其无法通过EXIST检查. 最后的SET VOLUMEID =%VOLUMEID ...行删除了结尾的反斜杠(由于某些原因,它算作两个字符),因此写为%MYDRIVE%\file*.*的路径看起来很合理.

I added the $ bogus value to be returned to fail an EXIST check. The final SET VOLUMEID=%VOLUMEID... line removes the trailing backslash (for some reason it counts as TWO characters) so that paths written as %MYDRIVE%\file*.* look sane.

推荐答案

这是最终的概念证明,无论谁觉得有用.调用GetVolumeID以"F:"形式将DRIVELETTER变量设置为返回值.

Here's a final proof of concept, for whomever finds it useful. Calling GetVolumeID sets a DRIVELETTER variable as return, in the "F:" form.

@ECHO OFF

CALL :GetVolumeID BACKUPDRIVE
SET "BACKUPDRIVE=%DRIVELETTER%"

CALL :GetVolumeID PENDRIVE
SET "PENDRIVE=%DRIVELETTER%"

if exist %PENDRIVE%\ (
    if exist %BACKUPDRIVE%\ (

        echo Emptying the pendrive.

        move %PENDRIVE%\*.* %BACKUPDRIVE%\pendrive\
    )
)

GOTO :eof

:GetVolumeID
SET DRIVELETTER=$$$
FOR /F "tokens=1,* delims==" %%A IN ('"wmic volume where label='%~1' get deviceid /value 2>nul"') DO  IF NOT "%%~B"=="" SET "DRIVELETTER=%%~B"
SET "DRIVELETTER=%DRIVELETTER:~0,-2%"
GOTO :eof

这篇关于如何在Windows路径中使用卷标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆