| 1 | unit UFileSystem;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Types, SysUtils;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TDirectory = class;
|
|---|
| 10 |
|
|---|
| 11 | TFilePath = class
|
|---|
| 12 | private
|
|---|
| 13 | FPath: string;
|
|---|
| 14 | function GetDirectory: string;
|
|---|
| 15 | function GetDrive: string;
|
|---|
| 16 | function GetName: string;
|
|---|
| 17 | procedure SetDirectory(const Value: string);
|
|---|
| 18 | procedure SetDrive(const Value: string);
|
|---|
| 19 | procedure SetExtension(const Value: string);
|
|---|
| 20 | procedure SetName(const Value: string);
|
|---|
| 21 | function GetExtension: string;
|
|---|
| 22 | public
|
|---|
| 23 | procedure Expand;
|
|---|
| 24 | procedure ExtractRelativePath(const BaseName, DestName: string);
|
|---|
| 25 | property Path: string read FPath write FPath;
|
|---|
| 26 | property Drive: string read GetDrive write SetDrive;
|
|---|
| 27 | property Directory: string read GetDirectory write SetDirectory;
|
|---|
| 28 | property Name: string read GetName write SetName;
|
|---|
| 29 | property Extension: string read GetExtension write SetExtension;
|
|---|
| 30 | end;
|
|---|
| 31 |
|
|---|
| 32 | TFile = class
|
|---|
| 33 | private
|
|---|
| 34 | Parent: TDirectory;
|
|---|
| 35 | FFile: File;
|
|---|
| 36 | function GetFileSize: Int64;
|
|---|
| 37 | procedure Remove;
|
|---|
| 38 | public
|
|---|
| 39 | AccessTime: TDateTime;
|
|---|
| 40 | CreateTime: TDateTime;
|
|---|
| 41 | ModifyTime: TDateTime;
|
|---|
| 42 | Name: string;
|
|---|
| 43 | property Size: Int64 read GetFileSize;
|
|---|
| 44 | function Exists: Boolean;
|
|---|
| 45 | procedure Rename(NewName: string);
|
|---|
| 46 | constructor Create;
|
|---|
| 47 | end;
|
|---|
| 48 |
|
|---|
| 49 | TFileSearch = class
|
|---|
| 50 | SearchRec: TSearchRec;
|
|---|
| 51 | function FindFirst: TFile;
|
|---|
| 52 | function FindNext: TFile;
|
|---|
| 53 | destructor Destroy; override;
|
|---|
| 54 | end;
|
|---|
| 55 |
|
|---|
| 56 | TFileSystem = class;
|
|---|
| 57 |
|
|---|
| 58 | TDirectory = class
|
|---|
| 59 | private
|
|---|
| 60 | FileSystem: TFileSystem;
|
|---|
| 61 | Parent: TDirectory;
|
|---|
| 62 | FFullPath: string;
|
|---|
| 63 | function GetFullPath: string;
|
|---|
| 64 | public
|
|---|
| 65 | Name: string;
|
|---|
| 66 | function CreateFile(Name: string): TFile;
|
|---|
| 67 | function CreateDirectory(Name: string): TDirectory;
|
|---|
| 68 | procedure RemoveDirectory(Directory: TDirectory);
|
|---|
| 69 | function FindFirstFile(Filter: string): TFileSearch;
|
|---|
| 70 | property FullPath: string read GetFullPath;
|
|---|
| 71 | function Exists: Boolean;
|
|---|
| 72 | end;
|
|---|
| 73 |
|
|---|
| 74 | TFileSystem = class
|
|---|
| 75 | private
|
|---|
| 76 | function GetSize: Int64;
|
|---|
| 77 | function GetFreeSpace: Int64;
|
|---|
| 78 | public
|
|---|
| 79 | Drive: Byte;
|
|---|
| 80 | function GetLetter: Char;
|
|---|
| 81 | function GetRootDirectory: TDirectory;
|
|---|
| 82 | function GetCurrentDirectory: TDirectory;
|
|---|
| 83 | procedure SetCurrentDirectory(Directory: TDirectory);
|
|---|
| 84 | property FreeSpace: Int64 read GetFreeSpace;
|
|---|
| 85 | property Size: Int64 read GetSize;
|
|---|
| 86 | end;
|
|---|
| 87 |
|
|---|
| 88 | implementation
|
|---|
| 89 |
|
|---|
| 90 | { TFileSystem }
|
|---|
| 91 |
|
|---|
| 92 | function TFileSystem.GetCurrentDirectory: TDirectory;
|
|---|
| 93 | begin
|
|---|
| 94 | Result := TDirectory.Create;
|
|---|
| 95 | Result.FileSystem := Self;
|
|---|
| 96 | Result.FFullPath := GetCurrentDir;
|
|---|
| 97 | end;
|
|---|
| 98 |
|
|---|
| 99 | function TFileSystem.GetFreeSpace: Int64;
|
|---|
| 100 | begin
|
|---|
| 101 | Result := DiskFree(Drive);
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | function TFileSystem.GetLetter: Char;
|
|---|
| 105 | begin
|
|---|
| 106 | Result := Chr(Ord('A') + Drive - 1);
|
|---|
| 107 | end;
|
|---|
| 108 |
|
|---|
| 109 | function TFileSystem.GetRootDirectory: TDirectory;
|
|---|
| 110 | begin
|
|---|
| 111 | Result := TDirectory.Create;
|
|---|
| 112 | Result.FileSystem := Self;
|
|---|
| 113 | Result.FFullPath := GetLetter + ':\';
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | function TFileSystem.GetSize: Int64;
|
|---|
| 117 | begin
|
|---|
| 118 | Result := DiskSize(Drive);
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | procedure TFileSystem.SetCurrentDirectory(Directory: TDirectory);
|
|---|
| 122 | begin
|
|---|
| 123 | SetCurrentDir(Directory.FFullPath);
|
|---|
| 124 | end;
|
|---|
| 125 |
|
|---|
| 126 | { TDirectory }
|
|---|
| 127 |
|
|---|
| 128 | function TDirectory.CreateDirectory(Name: string): TDirectory;
|
|---|
| 129 | begin
|
|---|
| 130 | Result := TDirectory.Create;
|
|---|
| 131 | Result.Parent := Self;
|
|---|
| 132 | Result.Name := Name;
|
|---|
| 133 | SysUtils.CreateDir(Result.FFullPath);
|
|---|
| 134 | end;
|
|---|
| 135 |
|
|---|
| 136 | function TDirectory.CreateFile(Name: string): TFile;
|
|---|
| 137 | begin
|
|---|
| 138 | Result := TFile.Create;
|
|---|
| 139 | Result.Parent := Self;
|
|---|
| 140 | Result.Name := Name;
|
|---|
| 141 | Result.Create;
|
|---|
| 142 | end;
|
|---|
| 143 |
|
|---|
| 144 | function TDirectory.Exists: Boolean;
|
|---|
| 145 | begin
|
|---|
| 146 | Result := DirectoryExists(FullPath);
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | function TDirectory.FindFirstFile(Filter: string): TFileSearch;
|
|---|
| 150 | begin
|
|---|
| 151 | Result := TFileSearch.Create;
|
|---|
| 152 | SysUtils.FindFirst(FullPath + filter, $2f, Result.SearchRec);
|
|---|
| 153 | end;
|
|---|
| 154 |
|
|---|
| 155 | function TDirectory.GetFullPath: string;
|
|---|
| 156 | begin
|
|---|
| 157 |
|
|---|
| 158 | end;
|
|---|
| 159 |
|
|---|
| 160 | procedure TDirectory.RemoveDirectory(Directory: TDirectory);
|
|---|
| 161 | begin
|
|---|
| 162 | SysUtils.RemoveDir(Directory.FFullPath);
|
|---|
| 163 | end;
|
|---|
| 164 |
|
|---|
| 165 | { TFile }
|
|---|
| 166 |
|
|---|
| 167 | constructor TFile.Create;
|
|---|
| 168 | begin
|
|---|
| 169 | end;
|
|---|
| 170 |
|
|---|
| 171 | function TFile.Exists: Boolean;
|
|---|
| 172 | begin
|
|---|
| 173 | Result := FileExists(Name);
|
|---|
| 174 | end;
|
|---|
| 175 |
|
|---|
| 176 | function TFile.GetFileSize: Int64;
|
|---|
| 177 | begin
|
|---|
| 178 | Result := FileSize(FFile);
|
|---|
| 179 | end;
|
|---|
| 180 |
|
|---|
| 181 | procedure TFile.Remove;
|
|---|
| 182 | begin
|
|---|
| 183 | Truncate(FFile);
|
|---|
| 184 | end;
|
|---|
| 185 |
|
|---|
| 186 | procedure TFile.Rename(NewName: string);
|
|---|
| 187 | begin
|
|---|
| 188 | RenameFile(Name, NewName);
|
|---|
| 189 | end;
|
|---|
| 190 |
|
|---|
| 191 | { TFileSearch }
|
|---|
| 192 |
|
|---|
| 193 | destructor TFileSearch.Destroy;
|
|---|
| 194 | begin
|
|---|
| 195 | FindClose(SearchRec);
|
|---|
| 196 | inherited;
|
|---|
| 197 | end;
|
|---|
| 198 |
|
|---|
| 199 | function TFileSearch.FindFirst: TFile;
|
|---|
| 200 | begin
|
|---|
| 201 |
|
|---|
| 202 | end;
|
|---|
| 203 |
|
|---|
| 204 | function TFileSearch.FindNext: TFile;
|
|---|
| 205 | begin
|
|---|
| 206 | SysUtils.FindNext(SearchRec);
|
|---|
| 207 | end;
|
|---|
| 208 |
|
|---|
| 209 | { TFilePath }
|
|---|
| 210 |
|
|---|
| 211 | procedure TFilePath.Expand;
|
|---|
| 212 | begin
|
|---|
| 213 | FPath := ExpandFileName(FPath);
|
|---|
| 214 | end;
|
|---|
| 215 |
|
|---|
| 216 | procedure TFilePath.ExtractRelativePath(const BaseName, DestName: string);
|
|---|
| 217 | begin
|
|---|
| 218 | Path := SysUtils.ExtractRelativePath(BaseName, DestName);
|
|---|
| 219 | end;
|
|---|
| 220 |
|
|---|
| 221 | function TFilePath.GetDirectory: string;
|
|---|
| 222 | begin
|
|---|
| 223 | Result := ExtractFilePath(Path);
|
|---|
| 224 | end;
|
|---|
| 225 |
|
|---|
| 226 | function TFilePath.GetDrive: string;
|
|---|
| 227 | begin
|
|---|
| 228 | Result := ExtractFileDrive(Path);
|
|---|
| 229 | end;
|
|---|
| 230 |
|
|---|
| 231 | function TFilePath.GetExtension: string;
|
|---|
| 232 | begin
|
|---|
| 233 | Result := ExtractFileExt(Path);
|
|---|
| 234 | end;
|
|---|
| 235 |
|
|---|
| 236 | function TFilePath.GetName: string;
|
|---|
| 237 | begin
|
|---|
| 238 | Result := ExtractFileName(Path);
|
|---|
| 239 | end;
|
|---|
| 240 |
|
|---|
| 241 | procedure TFilePath.SetDirectory(const Value: string);
|
|---|
| 242 | begin
|
|---|
| 243 |
|
|---|
| 244 | end;
|
|---|
| 245 |
|
|---|
| 246 | procedure TFilePath.SetDrive(const Value: string);
|
|---|
| 247 | begin
|
|---|
| 248 |
|
|---|
| 249 | end;
|
|---|
| 250 |
|
|---|
| 251 | procedure TFilePath.SetExtension(const Value: string);
|
|---|
| 252 | begin
|
|---|
| 253 |
|
|---|
| 254 | end;
|
|---|
| 255 |
|
|---|
| 256 | procedure TFilePath.SetName(const Value: string);
|
|---|
| 257 | begin
|
|---|
| 258 |
|
|---|
| 259 | end;
|
|---|
| 260 |
|
|---|
| 261 | end.
|
|---|