We help IT Professionals succeed at work.

Drive Letters

Medium Priority
Last Modified: 2012-06-21
I have a problem with the CD-ROM drive letter. I have a product running from a CD. This product does not allow you to call a file by just entering "folder\file.123" you have to enter the full path, "e:\work\folder\file.123".

As this is running from a CD this causes a problem, because the CD drive is not always e:, it could d:. I have found a way of creating a work drive using VB script. This pops up a prompt asking the user to specify what drive letter the CD is on and then it creates a duplicate drive called z: which is the sames as the CD (A bit like a mapped drive).

Heres is the script I used

Dim WinShell, prompt, folderpath, substcmd

Set WinShell = WScript.CreateObject("WScript.Shell")

prompt = "Type the path that you want to assign to drive w." & vbCrLf &_
"" & vbCrLf &_
"(Be sure to enclose the path in double quotes if it includes spaces.)"

folderpath = InputBox(prompt, "Where do you want to work today?")

If folderpath = "" Then
  substcmd ="subst w: " & folderpath

  WinShell.Run substcmd, 2, False

  'MsgBox "Ready?", 32, "Make Work Drive"

  'WinShell.Run "explorer.exe /n, /e, w:\",1,False
End If


There is also a script to remove the work drive. I have tried to put this into VB put have been unable because VB doesn't like the WScript.CreateObject("WScript.Shell"). After searching on the web it turns out Wscript can only be used in scripting languages.

Is there a way of doing this in VB. The reasons for wanting to do this is, 1) People are reluctant to run VB scripts 2) Doesn't look very good and 3) I'd like to have an autorun on the CD which u can choose to create the work drive.

Thanks in advance

Neil White
Watch Question

Head of Software Services
Neil, there are in fact many ways that you could achieve this. Lets first address the use of WScript in VB. You can use these objects BUT you have to modify the syntax slightly:

Set WinShell = Wscript.CreateObject("WScript.Shell")


Set WinShell = CreateObject("WScript.Shell")

The rest is the same.

You can use either API calls or File System Object to get the CD Drive, personally I would steer away from FSO as it adds additional overhead to the application for little benefit unless you are using it extensively. For an API solution:

Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Sub cmdListDrives_click()
    Dim strSave As String
    Dim lngReturn As Long
    Dim aryDrives() As String
    strSave = String(255, Chr$(0))
    lngReturn = GetLogicalDriveStrings(255, strSave)
    aryDrives = Split(strSave, Chr(0))
    For intDrive = LBound(aryDrives) To UBound(aryDrives)
        If Len(aryDrives(intDrive)) > 0 Then lstDrives.AddItem aryDrives(intDrive) & " - " & DriveType(aryDrives(intDrive))
End Sub

Private Function DriveType(ByVal DriveLetter As String) As String
    Select Case GetDriveType(DriveLetter)
        Case 2
            DriveType = "Removable"
        Case 3
            DriveType = "Drive Fixed"
        Case Is = 4
            DriveType = "Remote"
        Case Is = 5
            DriveType = "Cd-Rom"
        Case Is = 6
            DriveType = "Ram disk"
        Case Else
            DriveType = "Unrecognized"
    End Select
End Function


Excellent thanks, works fine!

If you don't mind could u tell me how to execute a text file from VB. I;ve tried the follwoing code with no sucess.

Private Sub butReadMe_Click()
Dim DocPath, WinShell

DocPath = "notepad.exe" & App.Path & "/files/GeoTechnical_Databases.txt"

Set WinShell = CreateObject("WScript.Shell")

WinShell.Run DocPath

End Sub

I get the message

Run Time error '70':

Permission denied


Do you mean open a text file in notepad?You can use the ShellExecute API.

' Declarations
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
Call ShellExecute(hwnd, "Open", ("C:\Textfile.txt"), "", App.Path, 1)
End Sub


doesn't work, nothing happens


Does what I want excellent
 Thanks you

Explore More ContentExplore courses, solutions, and other research materials related to this topic.