Floppy disk?Yes, there might be a situation when you will need to programmatically access and operate on the floppy disk, even today.
SetErrorMode The SetErrorMode API function controls how the operating system handles several types of serious errors. When SEM_FAILCRITICALERRORS is set the operating system does not display the critical-error-handler message box when such an error occurs. Instead, the operating system sends the error to the calling process.
DiskSize Function returns the size in bytes of the specified drive number, where 0 = Current, 1 = A, 2 = B, etc. DiskSize returns -1 if the drive number is invalid.
DiskFree Function returns the number of free bytes on the specified drive number, where 0 = Current, 1 = A, 2 = B, and so on. DiskFree returns -1 if the drive number is invalid.
ShFormatDrive SHFormatDrive function is not documented in the help files neither is it declared by Delphi. We will have to declare this function form the Shell32.dll. It provides access to the Widows format dialog box. To declare functions from a DLL, append the reserved word external and the name of the DLL to the end of a normal procedure or function header. You will also have to add calling convention of stdcall since we are using a WIN32 API DLL. As I am using this function only in this one unit, I declare it just before the procedure that will use it.
Is Disk in Drive?Before trying to save or load a file you can "quietly" check if a floppy drive has a floppy disk in it and present the user with a meaningful error message rather than just getting a critical error box that Windows displays.
procedure TForm1.Button1Click(Sender: TObject) ; var EMode: Word; begin EMode := SetErrorMode(SEM_FAILCRITICALERRORS) ; try if DiskSize(Ord('A')-$40) <> -1 then ShowMessage('Disk <b>in</b> drive A: !') else ShowMessage('No disk <b>in</b> drive A: !') ; finally SetErrorMode(EMode) ; end; end;
Disk free spaceIf you need to do a backup on a floppy disk, it is a good idea to check amount of free disk space.
procedure TForm1.Button2Click(Sender: TObject) ; var Drive: Byte; sFD, sSD : string; DFree, DSize : int64; begin Drive:=1; DFree:=DiskFree(Drive) ; DSize:=DiskSize(Drive) ; if (DFree <> -1) and (DSize <> -1) then begin sFD:='Disk Free: '+IntToStr(DFree div 1024)+' Kb'; sSD:='Disk Size: '+IntToStr(DSize div 1024)+' Kb'; ShowMessage(sFD + #13 + sSD) ; end; end;
Windows formatThe idea here is to handle disk formatting using the same dialog box that the Shell uses. If you want to, you can even silently use DOS format function.
Note: add ShellApi to unit's uses clause.
procedure TForm1.Button4Click(Sender: TObject) ; const SHFMT_DRV_A = 0; SHFMT_DRV_B = 1; SHFMT_ID_DEFAULT = $FFFF; SHFMT_OPT_QUICKFORMAT = 0; SHFMT_OPT_FULLFORMAT = 1; SHFMT_OPT_SYSONLY = 2; SHFMT_ERROR = -1; SHFMT_CANCEL = -2; SHFMT_NOFORMAT = -3; var FmtRes : LongInt; begin try FmtRes:=ShFormatDrive(Handle, SHFMT_DRV_A, SHFMT_ID_DEFAULT, SHFMT_OPT_QUICKFORMAT) ; case FmtRes of SHFMT_ERROR: ShowMessage('Error formatting the drive') ; SHFMT_CANCEL: ShowMessage('User canceled formatting the drive') ; SHFMT_NOFORMAT: ShowMessage('Drive is not formatable') else ShowMessage('Disk has been formatted') ; end; except ShowMessage('Error occurred!') end; end;
Final wordsIn some cases, you will need to handle floppy disk. These functions are here to help you, and to save the time and energy you might spend in searching for right solution. Don't panic if you don't understand some of the code here, try to play with it for a while, and you'll see that Windows API is not so hard as it seems.
Note: It is very easy to implement such procedures (some of them) to work with CD-ROM or any other removable media.