vbscript改变驱动器号

时间:2013-05-21 08:56:44

标签: vbscript

想要更改卷的驱动器号。一旦脚本运行,它应该在弹出框中显示分区并要求新的驱动器号。

在代码下方使用,但它只是将d更改为q

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colVolumes = objWMIService.ExecQuery _
("Select * from Win32_Volume Where Name = 'D:\\'")

For Each objVolume in colVolumes
objVolume.DriveLetter = "Q:"
objVolume.Put_
Next    

1 个答案:

答案 0 :(得分:0)

晚会还不到5年。这是一个方便的脚本,我使用上面的脚本编写,最初来自https://gallery.technet.microsoft.com/scriptcenter/ccae4d09-1bee-4f10-8c8c-176a53a65238

我经常插入和拔出USB驱动器,我有几个设备,我喜欢“永久”分配给特定的驱动器号。将此代码保存在文本文件中,并将其重命名为:ChangeDriveLetter.vbs

将文件放在计算机上的某个位置,然后在桌面或任何您想要的位置创建快捷方式。然后右键单击快捷方式并指定要在任何时候更改一两个驱动器号时用于打开它的键盘快捷键。

这适用于Windows 10

    ' Elevate the script to run As Administrator 

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
  WScript.Quit
End If

' Ask the user which drive to change, and what to change it to.
Sub subAskUser()

    strSourceDrive = InputBox("What Drive to Reassign","Enter the drive letter to reassign:","D")

    If ((strSourceDrive = vbCancel) Or (strSourceDrive = "") Or IsEmpty(strSourceDrive)) Then

        WScript.Quit

    End If

    strTargetDrive = InputBox("What Letter to Assign","Enter the new drive letter:","Q")

    If ((strTargetDrive = vbCancel) Or (strTargetDrive = "") Or IsEmpty(strTargetDrive)) Then

        WScript.Quit

    End If

    'If The letters are present and the user did not cancel, then call the Subroutine to change the letters
    subChangeLetter strSourceDrive, strTargetDrive

End Sub

subAskUser()

Sub subChangeLetter(strSourceDrive, strTargetDrive)

    'Check that the C: drive is not the letter that the user is trying to change from or to.
    If ((strSourceDrive <> "C") And (strSourceDrive <> "c") And (strTargetDrive <> "C") And (strTargetDrive <> "c")) Then

            'Uncomment the next lines to see the values entered from the InputBox
            'MsgBox strSourceDrive
            'MsgBox strTargetDrive

            strComputer = "."
            Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

            Set colVolumes = objWMIService.ExecQuery _
            ("Select * from Win32_Volume Where Name = '"& UCase(strSourceDrive) & ":\\'")

            For Each objVolume in colVolumes
            objVolume.DriveLetter = UCase( strTargetDrive) & ":"
            objVolume.Put_
            Next

    Else

        MsgBox "Ah, ah, ah! You can't change the C: drive, dude!",16

    End If

End Sub

strGoAgain = MsgBox("Change another drive letter?",4)

If strGoAgain = vbYes Then

    subAskUser()

End If

WScript.Quit