Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format 1

Status
Not open for further replies.

Helen1greece

Technical User
Jun 4, 2003
85
0
0
GR
Is there a way to do format to a drive trough visual basic commands? I tried with a batch file to do it but it doesn't work...
 
API Declarations

Option Explicit

'API calls
Private Declare Function SHFormatDrive Lib "shell32.dll" (ByVal hwnd As Long, ByVal Drive As Long, ByVal fmtID As Long, ByVal options As Long) As Long
Private Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

'Drive type constants
Public Const DRIVE_CDROM = 5
Public Const DRIVE_FIXED = 3
Public Const DRIVE_RAMDISK = 6
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_REMOVABLE = 2
Public Const SHFMT_ID_DEFAULT = &HFFFF ' Option bits for options parameter
Public Const SHFMT_OPT_FULL = 1
Public Const SHFMT_OPT_SYSONLY = 2


Module

Public Sub FormatFloppy(hWndOwner As Long, ByVal DriveLetter As String)
Dim DriveNum As Long
Dim DriveType As Long
Dim ret As Long

' Add the root path to the drive letter
DriveLetter = Left(DriveLetter, 1) & ":\"
' Convert the drive letter into the corresponding drive number, A=0, B=1...
DriveNum = Asc(UCase(DriveLetter)) - Asc("A")
DriveType = GetDriveType(DriveLetter)
' Check if the drive is a floppy drive
If DriveType = DRIVE_REMOVABLE Then
ret = SHFormatDrive(hWndOwner, DriveNum, SHFMT_ID_DEFAULT, SHFMT_OPT_FULL)
Else
MsgBox "This is not a floppy drive!", vbExclamation, "Format Floppy Disk"
End If
End Sub


Usage

'Usage:
Private Sub Command1_Click()
FormatFloppy Me.hwnd, "A"
End Sub

 
Thank you, but this only for removable disk. I want for every type of drive. Can give me something like that?
 
Look at the code more closely...

If DriveType = DRIVE_REMOVABLE Then
ret = SHFormatDrive(hWndOwner,DriveNum,SHFMT_ID_DEFAULT, SHFMT_OPT_FULL)
Else
MsgBox "This is not a floppy drive!",vbExclamation, "Format Floppy Disk"
End If

The above IF/THEN statement simply checks to see if the drive is removeable. To make all drives formattable simply take away the IF line, the ELSE line, the MSGBOX line and the END IF line. Be careful tho, because it will format at will. Here's what you would have left:

ret = SHFormatDrive(hWndOwner,DriveNum,SHFMT_ID_DEFAULT, SHFMT_OPT_FULL)

You can also specify options for the format in the line above...

SHFMT_OPT_SYSONLY 'system files only (boot disc)
SHFMT_OPT_FULL 'format drive
 
Is there a way to start format without any questions?I mean to start and end fully automatically without any question.
 
Are you a virus writer? Why else might you want to format a fixed drive without warning?

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
No, I am not!!! Just I get to angry when I want to do format on my PC and I have to answer to all answers! Is there a way to do it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top