Scanning for removable disk drives

Discussion in 'Software' started by ceal21c, Sep 26, 2006.

  1. ceal21c

    ceal21c Private E-2

    Hey Guys,

    I would like to add an option to a program that would allow me to scan a computer for any removable disk drives like floppy, zip and usb disks. Any ideas on where I should start? Thanx.
     
  2. matt.chugg

    matt.chugg MajorGeek

    in vb6 ?

    you were using fso earlier.

    use it again ;)

    Personally I stay away from the scripting object and use pure api but you'd probably find it simpler with fso
     
  3. ceal21c

    ceal21c Private E-2

    Hey matt, I think i've got fso commands pretty down pack but if you think that api is a better way of doing it i'm willing to give it a try. Thanx 4 the input.
     
  4. matt.chugg

    matt.chugg MajorGeek

    well with fso it would probably be something like this:

    Code:
        Dim fso As New Scripting.FileSystemObject
        
        Dim d As Scripting.Drive
        For Each d In fso.Drives
            Select Case d.DriveType
                Case 0 ' Unknown
                
                Case 1 ' Removable
                    MsgBox d.DriveLetter & ": is removable"
                
                Case 2 ' Fixed
                
                Case 3 ' Remote
                
                Case 4 ' CDRom
                
                Case 5 ' ramdrive
                
                Case Else
                    'This should never occur but who knows with vb6
            End Select
        Next
    
    You may not need the full case statment but rather just use an if
     
  5. matt.chugg

    matt.chugg MajorGeek

    Try something like this to do it with api, this would remove the denpendancy on the Microsoft Scripting Runtime

    Code:
    [COLOR=#0000FF]Private[/COLOR] Declare [COLOR=#0000FF]Function[/COLOR] GetDriveType [COLOR=#0000FF]Lib[/COLOR] [COLOR=#FF0000]"kernel32"[/COLOR] [COLOR=#0000FF]Alias[/COLOR] [COLOR=#FF0000]"GetDriveTypeA"[/COLOR] ([COLOR=#0000FF]ByVal[/COLOR] nDrive [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR]) [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Long[/COLOR]
    [COLOR=#0000FF]Private[/COLOR] Declare [COLOR=#0000FF]Function[/COLOR] GetLogicalDriveStrings [COLOR=#0000FF]Lib[/COLOR] [COLOR=#FF0000]"kernel32"[/COLOR] [COLOR=#0000FF]Alias[/COLOR] [COLOR=#FF0000]"GetLogicalDriveStringsA"[/COLOR] ([COLOR=#0000FF]ByVal[/COLOR] nBufferLength [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Long[/COLOR], [COLOR=#0000FF]ByVal[/COLOR] lpBuffer [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR]) [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Long[/COLOR]
    
    [COLOR=#0000FF]Private[/COLOR] [COLOR=#0000FF]Sub[/COLOR] Form_Load()
        [COLOR=#0000FF]Dim[/COLOR] DrivesString    [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR]
        [COLOR=#0000FF]Dim[/COLOR] DrvI            [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]Integer[/COLOR]
        [COLOR=#0000FF]Dim[/COLOR] Drive           [COLOR=#0000FF]As[/COLOR] [COLOR=#0000FF]String[/COLOR]
        
        DrivesString = [COLOR=#0000FF]String[/COLOR](255, Chr$(0))
        
        ret& = GetLogicalDriveStrings(255, DrivesString)
    
        [COLOR=#0000FF]For[/COLOR] DrvI = 1 [COLOR=#0000FF]To[/COLOR] 100
            [COLOR=#0000FF]If[/COLOR] Left$(DrivesString, InStr(1, DrivesString, Chr$(0))) = Chr$(0) [COLOR=#0000FF]Then[/COLOR] Exit [COLOR=#0000FF]For[/COLOR]
            Drive = Left$(DrivesString, InStr(1, DrivesString, Chr$(0)) - 1)
            DrivesString = Right$(DrivesString, Len(DrivesString) - InStr(1, DrivesString, Chr$(0)))
            
            Select [COLOR=#0000FF]Case[/COLOR] GetDriveType(Drive)
                [COLOR=#0000FF]Case[/COLOR] 2  [COLOR=#008800]'Removable[/COLOR]
                    MsgBox Drive & [COLOR=#FF0000]" is removable"[/COLOR]
                [COLOR=#0000FF]Case[/COLOR] 3  [COLOR=#008800]'Fixed[/COLOR]
                    MsgBox Drive & [COLOR=#FF0000]" is fixed"[/COLOR]
                [COLOR=#0000FF]Case[/COLOR] 4  [COLOR=#008800]'Remote[/COLOR]
                    MsgBox Drive & [COLOR=#FF0000]" is remote"[/COLOR]
                [COLOR=#0000FF]Case[/COLOR] 5  [COLOR=#008800]'Cd-Rom[/COLOR]
                    MsgBox Drive & [COLOR=#FF0000]" is CDDrive"[/COLOR]
                [COLOR=#0000FF]Case[/COLOR] 6  [COLOR=#008800]'Ram disk[/COLOR]
                    MsgBox Drive & [COLOR=#FF0000]" is Ram Disk"[/COLOR]
                [COLOR=#0000FF]Case[/COLOR] [COLOR=#0000FF]Else[/COLOR] [COLOR=#008800]'Unrecognized[/COLOR]
                    MsgBox Drive & [COLOR=#FF0000]" is unknown"[/COLOR]
            [COLOR=#0000FF]End[/COLOR] Select
        [COLOR=#0000FF]Next[/COLOR] DrvI
    [COLOR=#0000FF]End[/COLOR] [COLOR=#0000FF]Sub[/COLOR]
    
    
     
    Last edited: Sep 26, 2006
  6. ceal21c

    ceal21c Private E-2

    Thanx for your reply matt, I will definately be adding your suggestions to my code library but the following code worked good for me. I've attached it for everyone to have a look:

    Option Explicit

    Private Const DRIVE_UNKNOWN = 0
    Private Const DRIVE_NO_ROOT_DIR = 1
    Private Const DRIVE_REMOVABLE = 2
    Private Const DRIVE_FIXED = 3
    Private Const DRIVE_REMOTE = 4
    Private Const DRIVE_CDROM = 5
    Private Const DRIVE_RAMDISK = 6

    Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

    Private Sub Form_Load()

    Dim i As Long, DrvType As Long

    For i = 0 To 25
    DrvType = GetDriveType(Chr(65 + i) & ":")

    If DrvType = DRIVE_REMOVABLE Or DrvType = DRIVE_RAMDISK Then
    MsgBox Chr(65 + i) & ":"
    End If

    Next i

    End Sub
     
  7. matt.chugg

    matt.chugg MajorGeek

    that works, but its almost a bit dodgy lol, the getlogicaldrivestrings returns a list of drives so you don't have to loop form a to z but since you are using the api call it will just bork (stole your word kodo ;) ) when it comes to a non existant drive without you knowing. which might slow it down a tad but not a lot.

    You don't need to add 65 to the i value you can loop from anything to anything

    Code:
    For i = 65 to 90
    Next
    
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds