Guest User

Drive Letter to Desired.vbs

a guest
Oct 4th, 2020
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. ' "Drive Letter To Desired.vbs"
  2. ' The Volume Name must end with characters @X where the "X" is the Desired Drive Letter.
  3. ' Does not run on: Win2000, WinXP
  4.  
  5. Set SystemSet = GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
  6. For each System in SystemSet
  7. Next
  8.  
  9. Dim WshShell, FSO
  10.  
  11. Set WshShell = WScript.CreateObject("WScript.Shell")
  12. Set FSO = CreateObject("Scripting.FileSystemObject")
  13.  
  14. thisDrive = Left(WScript.ScriptFullName, 1) & ":"
  15.  
  16. Set d = FSO.GetDrive(FSO.GetDriveName(FSO.GetAbsolutepathname(thisDrive)))
  17. If(Left(Right(d.VolumeName,2),1))="@" Then
  18. desiredDrive = Right(d.VolumeName,1) & ":"
  19. Else
  20. MsgBox "The Desired Drive Letter is Not Specified." & Chr(13) & Chr(13) & "No Actions Taken." , vbOKOnly, WScript.ScriptFullName
  21. wscript.quit
  22. End If
  23.  
  24. If (thisDrive = desiredDrive) Then
  25. MsgBox "This Volume is Already Mounted as " & desiredDrive & " drive." & Chr(13) & Chr(13) & "No Actions Taken." , vbOKOnly, WScript.ScriptFullName
  26. wscript.quit
  27. End If
  28.  
  29. If (FSO.FolderExists(desiredDrive)) Then
  30. MsgBox "The drive letter " & desiredDrive & ": is in Use by Another Volume." & Chr(13) & Chr(13) & "No Actions Taken." , vbOKOnly, WScript.ScriptFullName
  31. wscript.quit
  32. End If
  33.  
  34. ' Get volume associated to the current drive letter.
  35. '
  36. Set objExec = WshShell.Exec("mountvol " & thisDrive & " /L")
  37. strVolume = Trim(objExec.StdOut.ReadLine())
  38. while objExec.Status = 0
  39. WScript.Sleep(100)
  40. Wend
  41.  
  42. ' Unmount the drive.
  43. '
  44. Set objExec = WshShell.Exec("mountvol " & thisDrive & " /D")
  45. while objExec.Status = 0
  46. WScript.Sleep(100)
  47. Wend
  48.  
  49. ' Mount the drive with the desired drive letter.
  50. '
  51. Set objExec = WshShell.Exec("mountvol " & desiredDrive & " " & strVolume)
  52. while objExec.Status = 0
  53. WScript.Sleep(100)
  54. Wend
  55.  
  56. 'If OS is W7, W8 or W10 then open the Windows Explorer again
  57. 'because the above closes it.
  58.  
  59. If InStr(osCaption, "Windows 7") Then
  60. WshShell.Run "explorer.exe" & " /e, " & desiredDrive
  61. ElseIf InStr(osCaption, "Windows 8") Then
  62. WshShell.Run "explorer.exe" & " /e, " & desiredDrive
  63. ElseIf InStr(osCaption, "Windows 10") Then
  64. WshShell.Run "explorer.exe" & " /e, " & desiredDrive
  65. End If
  66.  
  67. Set FSO=nothing
  68. Set WshShell=nothing
  69. wscript.quit
  70.  
Advertisement
Add Comment
Please, Sign In to add comment