从网络驱动器远程返回用户映射的驱动器号 [英] Return users mapped drive letter from network drive remote
问题描述
我正在尝试找到一种方法来从网络驱动器中检索用户映射的驱动器号.我在这里阅读了几篇文章,但它们似乎都与映射/取消映射驱动器有关,而我只需要知道用户已将该网络驱动器映射到哪个驱动器即可.
I'm trying to find a way to retrieve the users mapped drive letter from a network drive. I've read a few posts here but they all seem to be around mapping/unmapping drives, whereas I simply need to know what drive the user has that network drive mapped to.
在命令提示符下,我使用:净用途s:例如,将给我网络驱动器的名称(\ servershare \ file \ sharename \ shared),但是
From a command prompt I use: Net Use s: for example will give me the name of the networked drive (\servershare\file\sharename\shared), however
Net Use \\servershare\file\sharename\shared
仅给出命令已成功完成",在此我需要返回"S:".
just gives "The command completed successfully", where I need 'S:' returning.
如何获取特定网络驱动器的驱动器号?
How do I get the drive letter returned for a specific network drive?
推荐答案
这样的批处理文件将起作用:
A batch file like this will work:
@FOR /F "tokens=2" %%D IN ('net use ^| find ":" ^| find /I "\\SERVER\SHARE"') DO @ECHO Drive letter is %%D
注意:
- 用已知的驱动器号替换
ECHO驱动器号为%% D
,可能是由(
和<代码>).将\\ SERVER \ SHARE
替换为您要查找的共享. - 如果没有驱动器映射到共享,则
DO
之后的语句将永远不会执行.如果有多个驱动器映射到共享,则它将对每个驱动器执行一次. - 您可以跳过第一个
^ |.如果您确定没有驱动器号,则找到:"
管道(例如,使用net创建的连接使用\\ SERVER \ SHARE
而不是net使用S:\\ SERVER \ SHARE
或net使用* \\ SERVER \ SHARE
). - 如果您确定使用不同的字符大小写不会与共享建立任何连接,则可以跳过
/I
开关. - 两个
@
字符可防止命令在控制台上回显.另一种选择是使用@ECHO OFF'
开始批处理文件. - 如果直接在命令行上而不是从批处理文件中运行它,则将两个
%% D
替换为%D
.
- Replace
ECHO Drive letter is %%D
with whatever code you want now that the drive letter is known, possibly a multi-line code block surrounded by(
and)
. Replace\\SERVER\SHARE
with the share you're looking for. - If there's no drive mapped to the share, the statement after
DO
will never execute. If there's more than one drive mapped to the share, it will execute once for each drive. - You could skip the first
^| find ":"
pipe if you're sure there won't be any connections without a drive letter (e.g. created withnet use \\SERVER\SHARE
as opposed tonet use S: \\SERVER\SHARE
ornet use * \\SERVER\SHARE
). - You could skip the
/I
switch if you're sure there won't be any connections to the share using different character case. - The two
@
characters prevent the commands from echoing on the console. An alternative is to begin the batch file with@ECHO OFF'
. - If you're running this directly on the command line instead of from a batch file, replace the the two
%%D
with%D
.
这篇关于从网络驱动器远程返回用户映射的驱动器号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!