In Delphi, you can extract file information using the TFile
and TFileInfo
classes from the System.IOUtils
unit. These classes provide convenient methods for working with files. Here’s an example of how to extract file information:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
unit FileInfoExtractor; interface uses System.SysUtils, System.Classes, System.IOUtils; type TFileInfoExtractor = class class procedure ExtractFileInfo(const FileName: string); end; implementation { TFileInfoExtractor } class procedure TFileInfoExtractor.ExtractFileInfo(const FileName: string); var FileInfo: TFileInfo; begin try // Check if the file exists if FileExists(FileName) then begin // Create a TFileInfo object FileInfo := TFile.GetFileInfo(FileName); // Extract file information Writeln(‘File Name: ‘, FileInfo.Name); Writeln(‘File Size: ‘, FileInfo.Size, ‘ bytes’); Writeln(‘Creation Time: ‘, DateTimeToStr(FileInfo.CreationTime)); Writeln(‘Last Access Time: ‘, DateTimeToStr(FileInfo.LastAccessTime)); Writeln(‘Last Write Time: ‘, DateTimeToStr(FileInfo.LastWriteTime)); Writeln(‘Is Read Only: ‘, BoolToStr(FileInfo.IsReadOnly, True)); end else begin Writeln(‘File not found: ‘, FileName); end; except on E: Exception do begin Writeln(‘Error: ‘, E.Message); end; end; end; end. |
You can use this class in your main program to extract information from a file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
program FileInfoExtractorDemo; {$APPTYPE CONSOLE} uses System.SysUtils, FileInfoExtractor; begin try TFileInfoExtractor.ExtractFileInfo(‘C:\Path\To\Your\File.txt’); except on E: Exception do begin Writeln(‘Fatal error: ‘, E.Message); end; end; end. |
This example uses the TFile.GetFileInfo
method to obtain a TFileInfo
object, and then extracts various file information such as name, size, creation time, last access time, last write time, and whether the file is read-only. Adjust the paths and filenames according to your needs.
Leave a Reply