devxlogo

Format or Copy Diskettes Using Windows API

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist