unit
InfoFiles;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,registry;
Type
TFileInfo=
record
Exists:
boolean
;
Name:
String
;
ShortName:
String
;
NameNoExt:
String
;
Extension:
string
;
AssociatedFile:
string
;
Path:
string
;
ShortPath:
string
;
Drive:
string
;
CreateDate:TDateTime;
Size:
Int64
;
Attributes:
record
ReadOnly:
boolean
;
Hidden:
boolean
;
System:
boolean
;
Archive:
boolean
;
end
;
ModifyDate:TDateTime;
LastAccessDate:TDateTime;
end
;
Function
ReadFileInfo(FileName:
string
):TFileInfo;
implementation
Function
ReadFileInfo(FileName:
string
):TFileInfo;
var
ts:TSearchRec;
Function
FileTime2DateTime(FT:_FileTime):TDateTime;
var
FileTime:_SystemTime;
begin
FileTimeToLocalFileTime(FT, FT);
FileTimeToSystemTime(FT,FileTime);
Result:=EncodeDate(FileTime
.
wYear, FileTime
.
wMonth, FileTime
.
wDay)+
EncodeTime(FileTime
.
wHour, FileTime
.
wMinute, FileTime
.
wSecond, FileTime
.
wMilliseconds);
end
;
Function
AssociatedFile(FileExt:
string
):
string
;
var
key:
string
;
begin
With
TRegistry
.
create
do
try
RootKey:=HKEY_CLASSES_ROOT;
OpenKey(FileExt,
false
);
Key:=ReadString(
''
);
CloseKey;
OpenKey(key+
'\Shell\open\command'
,
false
);
result:=ReadString(
''
);
Closekey;
finally
free;
end
end
;
begin
Result
.
Name:=ExtractFileName(FileName);
Result
.
Extension:=ExtractFileExt(FileName);
Result
.
NameNoExt:=Copy(Result
.
Name,
1
,length(Result
.
Name)-length(Result
.
Extension));
Result
.
Path:=ExtractFilePath(FileName);
Result
.
Drive:=ExtractFileDrive(FileName);
Result
.
ShortPath:=ExtractShortPathName(ExtractFilePath(FileName));
if
lowercase(Result
.
Extension)<>
'.exe'
then
Result
.
AssociatedFile:=AssociatedFile(Result
.
Extension);
if
FindFirst(FileName, faAnyFile, ts)=
0
then
begin
Result
.
Exists:=
true
;
Result
.
CreateDate:=FileDateToDateTime(ts
.
Time);
Result
.
Size:=ts
.
FindData
.
nFileSizeHigh*
4294967296
+ts
.
FindData
.
nFileSizeLow;
Result
.
Attributes
.
ReadOnly:=(faReadOnly
and
ts
.
Attr)>
0
;
Result
.
Attributes
.
Hidden:=(faHidden
and
ts
.
Attr)>
0
;
Result
.
Attributes
.
System:=(faSysFile
and
ts
.
Attr)>
0
;
Result
.
Attributes
.
Archive:=(faArchive
and
ts
.
Attr)>
0
;
Result
.
ModifyDate:=FileTime2DateTime(ts
.
FindData
.
ftLastWriteTime);
Result
.
LastAccessDate:=FileTime2DateTime(ts
.
FindData
.
ftLastAccessTime);
Result
.
ShortName:=ts
.
FindData
.
cAlternateFileName;
Findclose(ts);
end
else
Result
.
Exists:=
false
;
end
;
end
.