tbred
asked on
Reading and Writing to the "A" Drive
How do you write source code to write to the A drive, read from the A drive, and format a floppy from the A drive? I know how to write a .bat file but can not make the connection to programming code.
Here is the code on How to Format Floppy Disk using API. Note -- This code can format your Hard
Disk as well, so you should be careful.
In Declaration
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
Add 2 command buttons named :
cmdFormat and cmdDiskCopy
Private 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" & vbCrLf & _
"drive! Format this drive?", 276, "SHFormatDrive Example")
Select Case RetFromMsg
Case 6 'Yes
' UnComment to do it...
'RetVal = SHFormatDrive(Me.hwnd, DriveNumber, 0&, 0&)
Case 7 'No
' Do nothing
End Select
End If
End Sub
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,DiskCopyRunDl l " _
& DriveNumber & "," & DriveNumber, 1) 'Notice space after
Else ' Just in case 'DiskCopyRunDll
RetFromMsg = MsgBox("Only floppies can" & vbCrLf & _
"be diskcopied!", 64, "DiskCopy Example")
End If
End Sub
Add 1 ListDrive name 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 If
End Sub
Disk as well, so you should be careful.
In Declaration
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
Add 2 command buttons named :
cmdFormat and cmdDiskCopy
Private 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" & vbCrLf & _
"drive! Format this drive?", 276, "SHFormatDrive Example")
Select Case RetFromMsg
Case 6 'Yes
' UnComment to do it...
'RetVal = SHFormatDrive(Me.hwnd, DriveNumber, 0&, 0&)
Case 7 'No
' Do nothing
End Select
End If
End Sub
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,DiskCopyRunDl
& DriveNumber & "," & DriveNumber, 1) 'Notice space after
Else ' Just in case 'DiskCopyRunDll
RetFromMsg = MsgBox("Only floppies can" & vbCrLf & _
"be diskcopied!", 64, "DiskCopy Example")
End If
End Sub
Add 1 ListDrive name 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 If
End Sub
Reading and writing to the A drive is the same as writing to any other drive... you have to open the file you are reading or writing using the OPEN statement. See the Help files on the OPEN Statement...
For examples:
Open "TESTFILE" For Input As #1
Open "TESTFILE" For Output As #1
Open "TESTFILE" For Binary Access Write As #1
Open "TESTFILE" For Binary Access Read As #1
Once you have the file open, to read, you can use the GET or INPUT statements.
For example:
Dim MyString, MyNumber
Open "TESTFILE" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Input #1, MyString, MyNumber ' Read data into two variables.
Debug.Print MyString, MyNumber ' Print data to Debug window.
Loop
Close #1 ' Close file.
To write to a file, you can use the PUT or PRINT statements.
For example:
Open "TESTFILE For Output As #1
Print #1, "Hello World"
Close #1
As for Formating the A drive, there are a couple of ways you can accomplish this...
You can use a shell to format the drive, like this:
shell "start format a:",vbNormalFocus
Or you can use the windows interface to format. Put the following in a module in your program:
Declare Function SHFormatDrive Lib "shell32" _
(ByVal hwnd As Long, ByVal Drive As Long, ByVal fmtID As Long, ByVal options As Long) As Long
Then you can call this to format the A Drive:
SHFormatDrive 0, 0, 0&, 0&
Hope this helps!
Cheers!
For examples:
Open "TESTFILE" For Input As #1
Open "TESTFILE" For Output As #1
Open "TESTFILE" For Binary Access Write As #1
Open "TESTFILE" For Binary Access Read As #1
Once you have the file open, to read, you can use the GET or INPUT statements.
For example:
Dim MyString, MyNumber
Open "TESTFILE" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Input #1, MyString, MyNumber ' Read data into two variables.
Debug.Print MyString, MyNumber ' Print data to Debug window.
Loop
Close #1 ' Close file.
To write to a file, you can use the PUT or PRINT statements.
For example:
Open "TESTFILE For Output As #1
Print #1, "Hello World"
Close #1
As for Formating the A drive, there are a couple of ways you can accomplish this...
You can use a shell to format the drive, like this:
shell "start format a:",vbNormalFocus
Or you can use the windows interface to format. Put the following in a module in your program:
Declare Function SHFormatDrive Lib "shell32" _
(ByVal hwnd As Long, ByVal Drive As Long, ByVal fmtID As Long, ByVal options As Long) As Long
Then you can call this to format the A Drive:
SHFormatDrive 0, 0, 0&, 0&
Hope this helps!
Cheers!
>>SHFormatDrive
¿What do the letters "SH" stand for?
¿What do the letters "SH" stand for?
vikiing,
SH stands for SHELL... Microsoft's reasoning... Because it's a exported entry point of SHELL32.DLL... But then they don't all start with SH...
Here are all the entry points in SHELL32.DLL (Wishing that EE displayed non-proportional font... Oh well):
Ordinal Entry Point Name
0001 0000cbe5 CheckEscapesA
0004 00023691 CheckEscapesW
0005 0001664e CommandLineToArgvW
0006 00030d09 Control_FillCache_RunDLL
000a 0002369e Control_RunDLL
000c 000236ab DllGetClassObject
0014 0003c50b DoEnvironmentSubstA
0024 0005c249 DoEnvironmentSubstW
0027 0005c27f DragAcceptFiles
0028 0005c27b DragFinish
002a 0003c42e DragQueryFile
0030 0005c2b5 DragQueryFileA
0033 00002d73 DragQueryFileAorW
0034 0001fda3 DragQueryFileW
004a 00002515 DragQueryPoint
004e 000208ac DuplicateIcon
0050 00001522 ExtractAssociatedIconA
0063 0000ee78 ExtractAssociatedIconExA
007a 0000a4d8 ExtractAssociatedIconExW
007b 00004d79 ExtractAssociatedIconW
0083 00012164 ExtractIconA
0085 00008452 ExtractIconEx
0088 00020d76 ExtractIconExA
0092 00001426 ExtractIconResInfoA
0094 00006c77 ExtractIconResInfoW
00b2 00008145 ExtractIconW
00b4 00007e18 ExtractVersionResource16W
00b8 00009f3b FindExecutableA
00b9 00007249 FindExecutableW
00ba 000014f1 FreeIconList
00bb 00001171 InternalExtractIconListA
00bc 00009a3a InternalExtractIconListW
00bd 00002539 OpenAs_RunDLL
00be 0000498a PrintersGetCommand_RunDLL
00bf 0000f10b RealShellExecuteA
00c0 0000129b RealShellExecuteExA
00c9 00023684 RealShellExecuteExW
00ca 00001143 RealShellExecuteW
00cb 0000111c RegenerateUserEnvironment
00cc 000111fd SHAddToRecentDocs
00cd 00027135 SHAppBarMessage
00ce 0000b7db SHBrowseForFolder
00cf 00012afc SHBrowseForFolderA
00d0 00008918 SHChangeNotify
00d1 0002cabe SHFileOperation
00d2 0002c80b SHFileOperationA
00d3 0002c2b5 SHFormatDrive
00d4 0001802d SHFreeNameMappings
00d5 00012afc SHGetDataFromIDListA
00d6 00017437 SHGetDesktopFolder
00d7 00002742 SHGetFileInfo
00d8 000236b8 SHGetFileInfoA
00d9 00023677 SHGetInstanceExplorer
00da 0002ca4a SHGetMalloc
00db 0002ca1c SHGetPathFromIDList
00dc 000098c7 SHGetPathFromIDListA
00dd 00031430 SHGetSpecialFolderLocation
00de 0002d035 SHHelpShortcuts_RunDLL
00df 000212e3 SHLoadInProc
00e0 00015916 SheChangeDirA
00e1 0003047c SheChangeDirExA
00e2 0002e81a SheChangeDirExW
00e3 00005da0 SheChangeDirW
00e4 0001f300 SheConvertPathW
00e5 000162ed SheFullPathA
00e6 00008674 SheFullPathW
00e7 000072ca SheGetCurDrive
00e8 00047511 SheGetDirA
00e9 0000f090 SheGetDirExW
00ea 00011038 SheGetDirW
00eb 0000b187 SheGetPathOffsetW
00ec 0001bd7b SheRemoveQuotesA
00ed 0005b3f5 SheRemoveQuotesW
00ee 0002c828 SheSetCurDrive
00ef 000270df SheShortenPathA
00f0 0000863a SheShortenPathW
00f1 000036ce ShellAboutA
00f2 00007dfa ShellAboutW
00f3 000235a9 ShellExecuteA
00f4 0002fa69 ShellExecuteEx
00f5 00030531 ShellExecuteExA
00f6 0001f830 ShellExecuteW
00f7 00027142 Shell_NotifyIcon
00f8 0000affb Shell_NotifyIconA
00f9 0000c05e Shl1632_ThunkData32
00fa 00010596 Shl3216_ThunkData32
Cheers!
SH stands for SHELL... Microsoft's reasoning... Because it's a exported entry point of SHELL32.DLL... But then they don't all start with SH...
Here are all the entry points in SHELL32.DLL (Wishing that EE displayed non-proportional font... Oh well):
Ordinal Entry Point Name
0001 0000cbe5 CheckEscapesA
0004 00023691 CheckEscapesW
0005 0001664e CommandLineToArgvW
0006 00030d09 Control_FillCache_RunDLL
000a 0002369e Control_RunDLL
000c 000236ab DllGetClassObject
0014 0003c50b DoEnvironmentSubstA
0024 0005c249 DoEnvironmentSubstW
0027 0005c27f DragAcceptFiles
0028 0005c27b DragFinish
002a 0003c42e DragQueryFile
0030 0005c2b5 DragQueryFileA
0033 00002d73 DragQueryFileAorW
0034 0001fda3 DragQueryFileW
004a 00002515 DragQueryPoint
004e 000208ac DuplicateIcon
0050 00001522 ExtractAssociatedIconA
0063 0000ee78 ExtractAssociatedIconExA
007a 0000a4d8 ExtractAssociatedIconExW
007b 00004d79 ExtractAssociatedIconW
0083 00012164 ExtractIconA
0085 00008452 ExtractIconEx
0088 00020d76 ExtractIconExA
0092 00001426 ExtractIconResInfoA
0094 00006c77 ExtractIconResInfoW
00b2 00008145 ExtractIconW
00b4 00007e18 ExtractVersionResource16W
00b8 00009f3b FindExecutableA
00b9 00007249 FindExecutableW
00ba 000014f1 FreeIconList
00bb 00001171 InternalExtractIconListA
00bc 00009a3a InternalExtractIconListW
00bd 00002539 OpenAs_RunDLL
00be 0000498a PrintersGetCommand_RunDLL
00bf 0000f10b RealShellExecuteA
00c0 0000129b RealShellExecuteExA
00c9 00023684 RealShellExecuteExW
00ca 00001143 RealShellExecuteW
00cb 0000111c RegenerateUserEnvironment
00cc 000111fd SHAddToRecentDocs
00cd 00027135 SHAppBarMessage
00ce 0000b7db SHBrowseForFolder
00cf 00012afc SHBrowseForFolderA
00d0 00008918 SHChangeNotify
00d1 0002cabe SHFileOperation
00d2 0002c80b SHFileOperationA
00d3 0002c2b5 SHFormatDrive
00d4 0001802d SHFreeNameMappings
00d5 00012afc SHGetDataFromIDListA
00d6 00017437 SHGetDesktopFolder
00d7 00002742 SHGetFileInfo
00d8 000236b8 SHGetFileInfoA
00d9 00023677 SHGetInstanceExplorer
00da 0002ca4a SHGetMalloc
00db 0002ca1c SHGetPathFromIDList
00dc 000098c7 SHGetPathFromIDListA
00dd 00031430 SHGetSpecialFolderLocation
00de 0002d035 SHHelpShortcuts_RunDLL
00df 000212e3 SHLoadInProc
00e0 00015916 SheChangeDirA
00e1 0003047c SheChangeDirExA
00e2 0002e81a SheChangeDirExW
00e3 00005da0 SheChangeDirW
00e4 0001f300 SheConvertPathW
00e5 000162ed SheFullPathA
00e6 00008674 SheFullPathW
00e7 000072ca SheGetCurDrive
00e8 00047511 SheGetDirA
00e9 0000f090 SheGetDirExW
00ea 00011038 SheGetDirW
00eb 0000b187 SheGetPathOffsetW
00ec 0001bd7b SheRemoveQuotesA
00ed 0005b3f5 SheRemoveQuotesW
00ee 0002c828 SheSetCurDrive
00ef 000270df SheShortenPathA
00f0 0000863a SheShortenPathW
00f1 000036ce ShellAboutA
00f2 00007dfa ShellAboutW
00f3 000235a9 ShellExecuteA
00f4 0002fa69 ShellExecuteEx
00f5 00030531 ShellExecuteExA
00f6 0001f830 ShellExecuteW
00f7 00027142 Shell_NotifyIcon
00f8 0000affb Shell_NotifyIconA
00f9 0000c05e Shl1632_ThunkData32
00fa 00010596 Shl3216_ThunkData32
Cheers!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanx, Mc Rider !!!
ASKER
I will help get started. Thank everyone for helping.
(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 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" & vbCrLf & _
"drive! Format this drive?", 276, "SHFormatDrive Example")
Select Case RetFromMsg
Case 6'Yes
' UnComment to do it...
'RetVal = SHFormatDrive(Me.hwnd, DriveNumber, 0&, 0&)
Case 7'No
' Do nothing
End Select
End If
End Sub