Find the letter assigned to the main disk

I am trying to find a function such as Environto find on which drive the main drive in my business was mapped to a particular PC.

Using the file path "G: \ Eworking \ SET \ Operations \ file" I know that my computer was displayed so that this file path is inside the G drive, but others may be displayed differently, so I would like to determine .

I tried using the if else method through the alphabet and do it if Dir([filepath]) thenearlier (see below), but I was wondering if there is a better way to do this?

Sub LoopThroughDrives()
sFilePath As String

sFilePath = ":\Eworking\SET\Operations\file"

If Dir("A" & sFilePath) > 0 Then
    msgbox ("It in drive A")
    Else
        If Dir("B" & sFilePath) > 0 Then
            msgbox ("It in drive B")
            Else
                If Dir("C" & sFilePath) > 0 Then
                    msgbox ("It in drive C")
                    Else
                        '...........................
                        'All other letters of the alphabet, checking each possibility
                        '...........................
                End If
        End If
End If

End Sub
+4
source share
2 answers

A For ... The next loop using the ASCII character seems appropriate.

dim c as integer
for c = 65 to 90
    If CBool(Len(Dir(Chr(c) & sFilePath))) Then
        msgbox "It in drive " & Chr(c)   '<~~msgbox, not magbox
        exit for
    end if
next c
if c > 90 then msgbox "never found"
+5

:

For Each d In CreateObject("Scripting.FileSystemObject").Drives
    Debug.Print d.DriveLetter, d.Path, d.ShareName
Next
+7

Source: https://habr.com/ru/post/1652891/


All Articles