Format or Copy Diskettes Using Windows API

The Win32 API includes a pair of functions that let you format and copy diskettes from your programs:

 Private Declare Function SHFormatDrive _        Lib "shell32" (ByVal hwnd As Long, _        ByVal Drive As Long, _        ByVal fmtID As Long, _        ByVal options As Long) As LongPrivate Declare Function GetDriveType _        Lib "kernel32" _        Alias "GetDriveTypeA" _        (ByVal nDrive As String) As Long

Add two command buttons to your form, named cmdDiskCopy and cmdFormatDrive, and place this codeinto their Click events:

 Private Sub cmdDiskCopy_Click()        ' DiskCopyRunDll takes two         ' parameters- From and To        Dim DriveLetter$, DriveNumber&, _                DriveType&        Dim RetVal&, RetFromMsg&        DriveLetter = UCase(Drive1.Drive)        DriveNumber = (Asc(DriveLetter) - _                65)        DriveType = GetDriveType_                (DriveLetter)        If DriveType = 2 Then  'Floppies, _                etc                RetVal = Shell_                        ("rundll32.exe " & _                        "diskcopy.dll," _                        & "DiskCopyRunDll " & _                        DriveNumber & "," & _                        DriveNumber, 1)         Else   ' Just in case                RetFromMsg = MsgBox_                        ("Only floppies can be " & _                        "copied", 64, _                        "DiskCopy Example")        End IfEnd SubPrivate Sub cmdFormatDrive_Click()        Dim DriveLetter$, DriveNumber&, _                DriveType&        Dim RetVal&, RetFromMsg%                DriveLetter = UCase(Drive1.Drive)        DriveNumber = (Asc(DriveLetter) - _                65)         ' Change letter to Number: A=0        DriveType = GetDriveType_                (DriveLetter)        If DriveType = 2 Then  _                'Floppies, etc                RetVal = SHFormatDrive(Me.hwnd, _                        DriveNumber, 0&, 0&)        Else                RetFromMsg = MsgBox_                        ("This drive is NOT a " & _                        "removeable drive! " & _                        "Format this drive?", _                        276, "SHFormatDrive Example")                If RetFromMsg = 6 Then                        ' UnComment to do it...                        'RetVal = SHFormatDrive_                                (Me.hwnd, _                                '   DriveNumber, 0&, 0&)                End If        End IfEnd Sub

Add one DriveListBox control named Drive1:

 Private Sub Drive1_Change()        Dim DriveLetter$, DriveNumber&, _                DriveType&        DriveLetter = UCase(Drive1.Drive)        DriveNumber = (Asc(DriveLetter) - _                65)        DriveType = GetDriveType_                (DriveLetter)        If DriveType <> 2 Then  _                'Floppies, etc                cmdDiskCopy.Enabled = False        Else                cmdDiskCopy.Enabled = True        End IfEnd Sub

Be careful: this function can even format the hard disk.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: