Delphi Files, Directories, Disks, I/O
A. Files B. Directories C. Disks/Drives D. File I/O E. Stream I/O F. Port I/O

(bookmark.gif (183 bytes) Bookmark)

Topic Description Unit

ASPI

Advanced (Adaptec) SCSI Programming Interface.  What is ASPI?

Also see SCSI below

Possibly useful for burning CDs directly?  Examples?

Non-Delphi:
Updating ASPI-for-Win32 for your Windows 95/98
www.datman.com/tbul/dmtb_028.htm
 

Sorting out the ASPI driver mess
www.ncf.carleton.ca/~aa571/aspi.htm 

 

Boot Sector Steve Schafer's UseNet Post about "How do I change the boot record on a diskette?" with solutions for both Win 95/98 and NT

See Floppy.PAS unit in Alpha32.ZIP donated by Dejan Maksimovic
FSBR = File System Boot Record
Functions:  AllocFloppyFSBR, ReadFloppyFSBR, FreeFloppyFSBR,
CreateFloppyBootRecord

Boot Partition for WinNT
www.winimage.com/bootpart.htm 

Non-Delphi:
The Boot Process
http://www.nondot.org/sabre/os/articles/TheBootProcess 

 
CD-R List of CD-R Libraries
www.stgsys.com/linksprog.html 
 
bookmark.gif (183 bytes)CD-ROM

Open/Close CD-ROM drawer, PC Magazine, Neil Rubenking, Feb 4, 1997, www.zdnet.com/pcmag/pctech/content/solutions/oe1603a.htm

Status of CD Rom Drawer
www.xploiter.com/programming/delphi/tips4.shtml#tip37

Eject CD-ROM Under NT (using DeviceIOControl)
Primoz Gabrijelcic's UseNet Post

Control the AutoPlay feature
http://delphifaq.com/fq/q2001.shtml 

Getting the serial number of the CD ROM
http://members.xoom.com/_XMCM/jescott/DelphiCodeTips2.html#CDSerialNumber

Does my CD-ROM Drive contain an Audio CD?   Borland's FAQ 2756D
How can I eject a CD-ROM in code?  Borland's FAQ 2740D
How can I close the CD Tray in code?  Borland's FAQ 2861D
Querying the Audio Cd Autorun feature of Win32   Borland's FAQ 1169D
Testing/setting the CD autorun option under Win32  Borland's FAQ 1175D
How can I get the serial number of an Audio CD?  Borland's FAQ 1555D

Non-Delphi:
CD-Recordable FAQ
www.fadden.com/cdrfaq/faq06.html#[6-6 

Autorun.inf Commands
http://msdn.microsoft.com/library/psdk/shellcc/shell/Shell_basics/Autoplay_cmds.htm 

 
Cluster Size Clusterstorleken:  GetClusterSize
http://www.jpldata.net/~magnus/delphiqa/articles/0253.html 

FAT Type and Cluster Size Depends on Logical Drive Size
http://support.microsoft.com/support/kb/articles/Q67/3/21.ASP 

Default Cluster Size for FAT and NTFS
http://support.microsoft.com/support/kb/articles/Q140/3/65.asp 

See FAT32 below

 
CRC
FileCheck.gif (1195 bytes) Calculate Cyclic Redundancy Code (a meta CRC32) of a disk volume.
efg's CRC FileCheck Lab Report
 
bookmark.gif (183 bytes)D3DiskSpaceKludge ScreenD3DiskSpaceKludge.jpg (9687 bytes)

Delphi 4 introduced an Int64 data type and directly supports DiskFree and DiskSize functions for disks with capacity > 2 GB.  Win 95 OSR2 or later, Win 98 or Win NT 4.0 or later, all support a GetDiskFreeSpaceEx API call for disks > 2 GB but Delphi 3 doesn't provide any direct support for this function.

Delphi 4 introduced a trick by defining GetDiskFreeSpaceEx to be a variable, which in effect is a pointer to the real GetDiskFreeSpaceExA function, or a BackFillDiskFreeSpaceEx function that uses the older GetDiskFreeSpaceA API call when GetDiskFreeSpaceEx is not available.

A DiskSpaceKludge.PAS unit provides the same basic functionality provided in Delphi 4 with Int64 variables by using the Comp data type in Delphi 3, which is also a 64-bit integer but is treated as a floating point variable.  The unit initialization in DiskSpaceKludge, just like in Delphi 4's SysUtils.PAS unit, assigns a value to the GetDiskFreeSpaceEx variable.

You can use the DiskSize and DiskFree functions in DiskSpaceKludge in Delphi 3 for disks > 2 GB (assuming you have the right operating system version) and then just get rid of it and use the native SysUtils.PAS in Delphi 4.  DiskSpaceKludge also exposes the GetDiskFreeSpaceEx variable so the API call can be used directly.

Complete Working Example:  D3DiskSpaceKludge.ZIP
(must compile in D3 but works in D4; not tested in D2)

 
Device Installer Setup and Device Installer API interface.  Project JEDI Conversion Library.
ftp://delphi-jedi.org/api/SetupAPI.zip 
 
DeviceIOControl See VWin32.PAS unit in Alpha32.ZIP donated by Dejan Maksimovic

Eject CD-ROM Under NT
Primoz Gabrijelcic's UseNet Post

FAT32 unit including function GetFat32FreeSpace using DeviceIOControl
Dr. Abimbola A. Olowofoyeku's (African Chief) UseNet Post

Format a Disk with Delphi:  Steve Schafer's UseNet Post

Abner Doon's UseNet Post with IOCTL unit

 
Disk Geometry See TBIOSParamBlock and GetDeviceParamBlock in Steve Schafer's UseNet Post
Xavier Pacheco's UseNet Post with DriveInfo
 
DiskFree D1-D3:
function DiskFree(Drive:  Byte):  LongInt;

D4-D5:
function DiskFree(Drive:  Byte):  Int64;

Drive:  0=Current, 1=A, 2=B, ...

function returns -1 if drive is invalid

See D3 Disk Space Kludge

SysUtils
DiskInDrive How to check to see if a drive is ready, Borland's TI 921D

DiskInDrive is used in efg's ShowDrives example

 
DiskInfo Xavier Pacheco's UseNet Post with DriveInfo  
DiskSize D1-D3:
function DiskSize(Drive:  Byte):  LongInt;

D4-D5:
function DiskSize(Drive:  Byte):  Int64;

Drive:  0=Current, 1=A, 2=B, ...

function returns -1 if drive is invalid

See D3 Disk Space Kludge

SysUtils
Disk Utilities Equalise - make files on a floppy or network drive match
FindDuplicates - find and remove those multiple DLLs and other files
ShowMan - disk space usage pie-chart
WinTidy - locate and remove those temporary files you never knew you had

www.davidtaylor.freeserve.co.uk/software/disk.html

 
DriveComboBox Peter Below's UseNet Post about how to avoid Error 21 while using DriveComboBox.

Patric Allen's UseNet Post about how to avoid Error 21

TIEDriveComboBox is a combobox for drives with icons taken from the system imagelist and variable volume label  style.  Part of Filemanager Toolset.  http://delphi.icm.edu.pl/ftp/d40free/drivedir.zip

 
DriveType Todd M. Eischeid's DriveType function and DriveTypeToString  
Drive Info TDriveInfo is a non-visible class for the centralized drive management. It provides informations about the currently installed logical drives.   Part of Filemanager Toolset.  http://delphi.icm.edu.pl/ftp/d40free/drivedir.zip

Also see ShowDrives

 
Drive View TDriveView displays the directory structure of all  drives as treeview with shell icons taken from the system  imagelist. It supports file operations like creating, copying,  renaming and deleting directories, full OLE-drag & drop for files and directories using dragimages, clipboard operations,   detecting filesystem changes, displaying the system contextmenu and the system property sheets.   Part of Filemanager Toolset.   http://delphi.icm.edu.pl/ftp/d40free/drivedir.zip  
Drives "Directories and Drives," Delphi 5 Developer's Guide (pp. 380-403) by Teixeria and Pacheco  
Eject

Eject CD-ROM Under NT (using DeviceIOControl)
Primoz Gabrijelcic's UseNet Post

Ejecting a ZIP disk (Un exemple fourni par J.M. Pierrard : EJECTER une disquette ZIP ou un CD. Cette nouvelle version fonctionne aussi en NT.)
http://perso.wanadoo.fr/bardou/michel/zip/zipejecter.zip 

How can I eject a CD-ROM in code?  Borland's FAQ 2740D

 
ExtractFileDrive function ExtractFileDrive(const FileName: string): string;
Extracts the drive part of the given filename. For filenames with drive letters, the resulting string is '<drive>:'.  For filenames with a UNC path, the resulting string is in the form '\\<servername>\<sharename>'.
SysUtils
FAT File allocation table

See Floppy.PAS unit in Alpha32.ZIP donated by Dejan Maksimovic
Function:  CreateFloppyFATs

 
bookmark.gif (183 bytes)FAT32 see Cluster Size

FAT32 unit including functions GetFat32FreeSpace, IsFat32Available, FreeSpaceOnDrive, TotalSpaceOnDrive
Dr. Abimbola A. Olowofoyeku's (The African Chief)  UseNet Post

Angus Johnson's UseNet Post about FAT and cluster size and D1 GetClusterSizeFAT32 function:

"the GetDiskFreeSpace function is reliable in deriving cluster size if it is a FAT16 drive.  If it is a FAT32 drive, i.e., Win95b and Win98, then GetDiskFreeSpace is not reliable.  Most FAT32 drives will be formatted to a 4k cluster size but not all."

"DeviceIOControl can be used to derive the cluster size, there are a couple of function codes which can be used... Get_ExtDPB and Get_ExtFreeSpace."

Non-Delphi:

FAT32 File System
www.maverick-os.dk/FileSystemFormats/FAT32_FileSystem.html 

FAT 32 API
http://msdn.microsoft.com/library/psdk/winbase/fat32_12nk.htm

 
FDISK Non-Delphi:
How to Use Fdisk and Format to Partition or Repartition a Hard Disk
http://support.microsoft.com/support/kb/articles/q255/8/67.asp
 
Files To get a list or process all files on a drive, see the "recursive" solutions under FindFirst/FindNext/FindClose on Directories page.

The FileListLibrary.PAS unit provides a ScanDirectory procedure for a generic way to process a hierarchy of directories and files.    Two callback routines are parameters to ScanDirectory to process each file, and to process the beginning and end of a directory.  The routines ProcessDirectory and ProcessFile in ScreenFileCheck.PAS are the routines used as parameters to ScanDirectory.  See the FileCheck Lab Report for the source code.

 
Floppy See Floppy.PAS unit in Alpha32.ZIP donated by Dejan Maksimovic Functions:  AllocFloppyFSBR, ReadFloppyFSBR, FreeFloppyFSBR, CreateFloppyBootRecord, CreateFloppyFATs, CreateFloppyRootDir, FormatFloppyDisk

UseNet Post by Paul Jackson with FloppyDriveHasDisk example

 
bookmark.gif (183 bytes)Format Disk "Formatting Disks" in From the Shell:  Part I: Dialog Boxes You’ve Always Needed, Delphi Informant, April 1999

Peter Below's UseNet Post about using Quick Format
Krasimir Krastev's UseNet Post
Steve Schafer's UseNet Post:  DeviceIOControl example
Jong-Taek Ryu's UseNet Post with SHFormatDrive example
Xavier Pacheco's (TeamB) UseNet Post with caution and method using WinExec

Comment FORMATER UNE DISQUETTE en utilisant l'API SHFormatDrive.
http://perso.wanadoo.fr/bardou/michel/zip/formatdrive.zip 

Formatting a drive under Win32, Borland FAQ 1307D

Jon Scott's Format Floppy example
http://members.xoom.com/_XOOM/jescott/DelphiCodeTips1.html#FormatFloppy

See Floppy.PAS unit in Alpha32.ZIP donated by Dejan Maksimovic

ANTS TFloppyFormat component allows the floppy disks formatting and/or verification, disk checking, bootable disks making, single tracks formatting, reading/writing the floppy disk sectors in Windows 95, 98 and NT. Extensive help file and demonstration program are included.  http://delphi.icm.edu.pl/ftp/d20share/fformdem.zip

 
bookmark.gif (183 bytes)GetDiskFreeSpace FreeClusters and TotalClusters returned by GetDiskFreeSpace will be at most 65526 and the free space can never exceed 2,147,155,968 bytes.  This is a limitation of the Win32 API.  See GetDiskFreeSpaceEx.

See D3 Disk Space Kludge

FUNCTION FreeSpace(Drive: BYTE): Integer;
  VAR
    BytesPerCluster  : Integer;
    RootPath         : ARRAY[0..4] OF CHAR;
    RootPtr          : pChar;
    SectorsPerCluster:  DWORD ;    // for both D3 and D4
    BytesPerSector   :  DWORD;
    FreeClusters     :  DWORD;
    TotalClusters    :  DWORD;
BEGIN
  RootPtr := NIL;
  IF   Drive > 0
  THEN BEGIN
    StrCopy(RootPath, 'A:\');
    RootPath[0] := CHR(Drive + ORD('A') - 1);
    RootPtr := RootPath
  END;

  IF   GetDiskFreeSpace(RootPtr, SectorsPerCluster,
         BytesPerSector, FreeClusters, TotalClusters)
  THEN BEGIN
    BytesPerCluster := SectorsPerCluster * BytesPerSector;

    RESULT := BytesPerCluster * FreeClusters
  END
  ELSE RESULT := -1
END {FreeSpace};

System
GetDiskFreeSpaceEx
(D4)
This Win32 function only exists on Win NT 4.0 and Win 95 OSR2
and Win 98.  GetDiskFreeSpaceEx is actually a variable in SysUtils
that is set at startup to point to either the actual OS API function,
if it exists, or an internal Delphi function if it does not.  While
running on Win 95 pre-OSR2 the result is still limited to 2 GB,
but you don't have to worry about which API function to code.

See D3 Disk Space Kludge

procedure TForm1.ButtonShowSpaceClick(Sender: TObject);
  VAR
    FreeBytesAvailableToCaller:  TLargeInteger;    // Int64
    TotalNumberOfBytes        :  TLargeInteger;
    TotalNumberOfFreeBytes    :  TLargeInteger;
begin
  IF GetDiskFreeSpaceEx('C:',
                        FreeBytesAvailableToCaller,
                        TotalNumberOfBytes,
                        @TotalNumberOfFreeBytes)
  THEN BEGIN
  ShowMessage(
        'FreeAvailable = ' +
        IntToStr(FreeBytesAvailableToCaller) + 
        #$0D +
        'TotalSpace = '              +
        IntToStr(TotalNumberOfBytes) + 
        #$0D +
        'TotalFree = '               + 
        IntToStr(TotalNumberOfFreeBytes) );
  END
  ELSE ShowMessage('Invalid Disk')
end;
SysUtils
bookmark.gif (183 bytes)GetDriveType How to tell what kind of drive is used, Borland's TI 980D

Trouver le TYPE D'UN DRIVE (si c'est un disque dur, un lecteur de disquette etc.).
http://perso.wanadoo.fr/bardou/michel/zip/getdrivetype.zip 

efg's ShowDrives example

 
GetFat32FreeSpace FAT32 unit including function GetFat32FreeSpace using DeviceIOControl
Dr. Abimbola A. Olowofoyeku's (African Chief) UseNet Post
 
GetLogicalDrives

Neil Rubenking's GetLocalDrives in the AllFuncs.pas unit of his ColorClue utility.

efg's ShowDrives example

Windows
bookmark.gif (183 bytes)GetVolumeInformation

Neil Rubenking's GetVolumeSerial in the AllFuncs.pas unit of his ColorClue utility.

efg's ShowDrives example

FileSystemFlags returned by GetVolumeInformation include:
FS_CASE_IS_PRESERVED, FS_CASE_SENSITIVE, FS_UNICODE_STORED_ON_DISK, FS_PERSISTENT_ACLS,
FS_FILE_COMPRESSION, FS_VOL_IS_COMPRESSED

GetVolumeInformation
http://msdn.microsoft.com/library/psdk/winbase/fsys_6wfi.htm
 

See IsDriveCompressed example below.

 
IDE see book entry under SCSI below  
IOCTL Abner Doon's UseNet Post with IOCTL unit  
IsCDRom Detecting a CD-ROM Drive
www.undu.com/dn960223/00000011.htm
 
IsDiskWriteProtected See efg's ShowDrives example  
IsDriveCompressed
// 32 bin version (D2)
FUNCTION IsDriveCompressed(CONST drive: CHAR): BOOLEAN;
  VAR
    Buffer      : array[0..MAX_PATH] of CHAR;
    NotUsed : INTEGER;
    VolFlags : INTEGER;
BEGIN
  result := false;
  IF   GetVolumeInformation( PChar(Drive + ':\'), Buffer,
           SizeOf(Buffer), NIL, NotUsed, VolFlags, NIL, 0)
  THEN BEGIN
    IF   VolFlags and FS_VOL_IS_COMPRESSED <> 0
    THEN result := TRUE
  END
END;

See GetVolumeInformation above for other VolFlags

 
IsSubst Neil Rubenking's IsSubst in the AllFuncs.pas unit of his ColorClue utility.  Is a Subst drive.  
Managing Disks Delphi in a Nutshell,  p. 500  
Mapping Alberto Spelta's UseNet Post About How Show The Network Drive Map Dialog.  
Network Drives How do I map a network drive in Windows NT or '95?, Borland's TI 1592D

GetNetworkDriveMappings using WNetGetConnection
http://members.nbci.com/_XMCM/jescott/DelphiCodeTips3.html
#GetNetworkDriveMappings
 

Nework Share, Delphi Informant, Aug. 1999.

From the Shell:  Dialog Boxes You've Always Needed, Delphi Informant, May 1999, Vol. 5, No. 5, pp. 28-36

Function to list all the network drive mappings
www.chami.com/tips/delphi/112896D.html 

 
Out of Space From the Shell:  Dialog Boxes You've Always Needed, Delphi Informant, May 1999, Vol. 5, No. 5, pp. 28-36  
Ready See DiskInDrive  
SCSI ProjectJediButton.gif (3350 bytes)  ASPI and SCSI for Win32 Definitions.  Project JEDI conversion library by Eugene Danilov.  
ftp://delphi-jedi.org/api/scsi.zip 
http://www.delphi-jedi.org/Jedi:APILIBRARY 

  
US  UK  DE 

 
bookmark.gif (183 bytes)Serial Numbers Steve Schafer's (Team B) 8 Oct 99 UseNet Post about "Serial Number on Disk":
"A hard disk has two serial numbers. One is the hardware serial number (which is sometimes displayed by the BIOS during the boot-up process).  The hardware serial number never changes. The other is the volume serial number (the one that is displayed when you issue a DIR command at the DOS prompt). The volume serial number changes whenever you format the disk."

Mike's UseNet Post with two methods to get Volume Label

Peter Below's (Team B) UseNet Post about changing drive's serial number (16-bit code)
Steve Schafer's (Team B) UseNet Post about changing drive's serial number

Getting and setting a disks (sic) serial number, Borland's TI 475D
Getting / Setting diskette serial # under Windows, Borland TI 521D
Managing disk volume labels in Delphi (D1), Borland TI 904D

Getting the serial number of the CD ROM
http://members.xoom.com/_XMCM/jescott/DelphiCodeTips2.html#CDSerialNumber

 
ShChangeNotify

Shell Notifications:  Getting Windows to Share Some of Its Secrets, Delphi Informant, April 1999.

Peter Below's UseNet Post:  If present the drive can signal to the drive controller that a media change occurred, the controller informs the driver and the driver sends a WM_DEVICECHANGE message around to inform any interested parties of the change. Cheaper drives often don't have the hardware.

Uses ShlObj;
 
  ShChangeNotify(
    SHCNE_MEDIAINSERTED,
    SHCNF_PATH,
    Pchar('A:\'),
    Nil );
  Sleep( 1000 ); 

 
SHFormatDrive See "Format Disk Drive" (above)  
ShowDriveType How to tell what kind of drive is used, Borland's TI 980D  
bookmark.gif (183 bytes)ShowDrives Procedure to display drives in TMemo with fixed-width "Terminal" font..  Uses GetLogicalDrives procedure to indentify existing drives and GetVolumeInformation, DiskFree and DiskSize to display drive information.  Limited to 2 GB in D3 but there is no limit in D4 or later.  ShowDrives.TXT

Sample Output:
Disks

    Drive   R/W   Total Bytes       Free Bytes    Vol Ser     Label
  --------- --- ---------------- ---------------- -------- -------------
  A Floppy  R/O        1,457,664        1,444,864 B4024F70 DRIVERS
  C Fixed   R/W   10,230,431,744    7,687,897,088 15FD3658 C
  D Fixed   R/W    3,222,343,680      508,985,344 3B590FD8 D
  E CDROM   R/W      686,587,904      651,788,288 06842D93 Oct2000
  F CDROM   R/O      530,153,472                0 73CF38F6 JoCo
  J Network R/O   12,057,051,136    2,213,543,936 00000000 EFG
  S Network R/W    5,242,748,928      858,783,744 00000000 Server
  Z Network       <disk missing>                            

List of valid drives (Lister la liste DRIVES VALIDES)
http://perso.wanadoo.fr/bardou/michel/zip/listedrives.zip 

subst Howard Moon's UseNet Post describing a "DOS" way to assign a drive letter to a directory  
SWAG Drive  SWAG (Software Archive Group).  124 various disk/drive examples
www.gdsoft.com/swag/drives.zip   (requires Reader)
 
TDriveType efg's ShowDrives example FileCtrl
TDVDrivesCombo TDVDrivesCombo & TDVDrivesList Two Delphi components displays drives combo box or list box with associated icons.   http://delphi.icm.edu.pl/ftp/d20free/dvdrives.zip  
bookmark.gif (183 bytes)WM_DeviceChange

efg's FileCheck Lab Report

ProjectJediButton.gif (3350 bytes)  Equates for WM_DEVICECHANGE and BroadcastSystemMessage.  Project JEDI Conversion Library by Tom Deprez.
http://www.delphi-jedi.org/Jedi:APILIBRARY 
ftp://delphi-jedi.org/api/dbt.zip 

DBT_H
WNetGetConnection See Network Drives  
Zip Disk Ejecting a ZIP disk (Un exemple fourni par J.M. Pierrard : EJECTER une disquette ZIP ou un CD. Cette nouvelle version fonctionne aussi en NT.)
http://perso.wanadoo.fr/bardou/michel/zip/zipejecter.zip 
 

Updated
  27 Aug 2005


since
 19 Apr 1999