VB and VBA Users Source Code: Format a floppy/removeable disk
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Format a floppy/removeable disk
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, June 29, 2000
Hits:
1470
Category:
Windows API
Article:
Below is the code to format a floppy disk using API calls: Option Explicit 'API Private Declare Function SHFormatDrive Lib "shell32" (ByVal hwnd As Long, ByVal Drive As Long, ByVal fmtID As Long, ByVal options As Long) As Long Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long Private Declare Function GetActiveWindow Lib "user32" () As Long 'Constants Private Const DRIVE_CDROM = 5, DRIVE_FIXED = 3 Private Const DRIVE_RAMDISK = 6, DRIVE_REMOTE = 4 Private Const DRIVE_REMOVABLE = 2, SHFMT_ID_DEFAULT = &HFFFF Private Const SHFMT_OPT_FULL = 1, SHFMT_OPT_SYSONLY = 2 'Purpose : Format a Floppy Disk or other removeable drive 'Inputs : [sDriveLetter] The drive letter (usually "A") 'Outputs : Returns -1 if the drive specified isn't a floppy drive ' Returns -2 if the user pressed cancel ' Returns 6 if the format succeeded 'Author : Andrew Baker 'Date : 09/12/2000 03:05 'Notes : 'Revisions : Public Function FormatRemoveableDrive(Optional ByVal sDriveLetter As String = "A") As Long Dim lDriveNum As Long Dim lDriveType As Long, lhWndOwner As Long On Error GoTo ErrFailed 'Add the root path to the drive letter sDriveLetter = Left$(sDriveLetter, 1) & ":\" lhWndOwner = GetActiveWindow 'Convert the drive letter into the corresponding drive number lDriveNum = Asc(UCase$(sDriveLetter)) - Asc("A") lDriveType = GetDriveType(sDriveLetter) If lDriveType = DRIVE_REMOVABLE Then 'Drive is a floppy drive FormatRemoveableDrive = SHFormatDrive(lhWndOwner, lDriveNum, SHFMT_ID_DEFAULT, SHFMT_OPT_FULL) Else 'Drive is not a Floppy disk drive FormatRemoveableDrive = -1 End If Exit Function ErrFailed: 'Error Handler FormatRemoveableDrive = Err.LastDllError On Error GoTo 0 End Function 'Demonstration routine Sub Test() Dim lRet As Long lRet = FormatRemoveableDrive("A") If lRet = -1 Then 'Failed MsgBox "Drive A is Not a Floppy" ElseIf lRet = 6 Then 'Succeed MsgBox "Formated Drive A" Else MsgBox "Failed" End If End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder