如何从批处理文件中获取驱动器列表?

发布于 2024-07-18 10:06:57 字数 104 浏览 12 评论 0原文

我正在尝试获取用于系统升级的所有映射驱动器的列表,并希望通过批处理文件获取此信息。 我怎样才能做到这一点?

对于奖励积分: 如何编写新服务器上这些驱动器映射的脚本?

I'm trying to get a list of all my mapped drives for a system upgrade and would like to get this info via a batch file. How can I do this?

For Bonus points:
How can I script the mapping of these drives on the new server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-07-25 10:06:58

当前映射的列表将由以下方式返回

net use

传输将像这样工作(为了有趣1,让我们在批处理脚本而不是 VBScript 中执行此操作):

@echo off

setlocal EnableDelayedExpansion

set letter=.
set uncpath=.
set colon=.

for /f "delims=" %%l in ('net use') do @(
  for /f "tokens=2" %%t in ("%%l") do @set letter=%%t
  for /f "tokens=3" %%t in ("%%l") do @set uncpath=%%t

  set colon=!letter:~1,1!

  if "!colon!" EQU ":" (
    echo if exist !letter! net use !letter! /delete
    echo net use !letter! !uncpath! /persistent:yes
  )
)

endlocal

输出如下所示:

if exist M: net use M: /delete
net use M: \\someserver\someshare /persistent:yes
if exist N: net use N: /delete
net use N: \\otherserver\othershare /persistent:yes

只需存储该内容在批处理文件中,您就可以开始了。


1 事实上,“有趣”这个词用在这里并不恰当。 ;-)

The list of current mappings will be returned by

net use

The transfer would work like this (for the fun1 of it, lets do that in batch script instead of VBScript):

@echo off

setlocal EnableDelayedExpansion

set letter=.
set uncpath=.
set colon=.

for /f "delims=" %%l in ('net use') do @(
  for /f "tokens=2" %%t in ("%%l") do @set letter=%%t
  for /f "tokens=3" %%t in ("%%l") do @set uncpath=%%t

  set colon=!letter:~1,1!

  if "!colon!" EQU ":" (
    echo if exist !letter! net use !letter! /delete
    echo net use !letter! !uncpath! /persistent:yes
  )
)

endlocal

output goes something like this:

if exist M: net use M: /delete
net use M: \\someserver\someshare /persistent:yes
if exist N: net use N: /delete
net use N: \\otherserver\othershare /persistent:yes

Just store that in a batch file and you are good to go.


1 Actually, "fun" is not the right word here. ;-)

不可一世的女人 2024-07-25 10:06:58

列出映射的驱动器

Net Use

映射新驱动器

Net Use G: \\Server\Path

To list the mapped drives

Net Use

To map a new drive

Net Use G: \\Server\Path
是伱的 2024-07-25 10:06:58

您可以使用 vbscript 来完成此操作

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

For Each objDrive in colDrives
    Wscript.Echo "Drive letter: " & objDrive.DriveLetter
Next

并且可以使用以下命令映射网络驱动器

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive "G:", "\\Server\Path"

You can do this using vbscript

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

For Each objDrive in colDrives
    Wscript.Echo "Drive letter: " & objDrive.DriveLetter
Next

And you can map network drives with

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive "G:", "\\Server\Path"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文