//文件时间转DateTime
function FileTimeToDateTime(const AFileTime:TFileTime):TDateTime;
var
SYSTime:TSystemTime;
begin
if FileTimeToSystemTime(AFileTime,SYSTime) then
Result:=SystemTimeToDateTime(SYSTime)
else Result:=0;
end;
function PathRelativePathTo(pszPath:LPSTR; pszFrom:LPCSTR; dwAttrFrom:DWORD;
pszTo:LPCSTR; dwAttrTo:DWORD):BOOL; stdcall;
external "shlwapi.dll " name "PathRelativePathToA ";
//取得相对路径
function GetFileRelativePath(CurPath:string; const Dest:string; const NoDot:Boolean=False):string;
var
ResultPath: array[0..1024] of Char;//路径转换结果
begin
CurPath:=ExtractFileDir(CurPath)+ " ";
if PathRelativePathTo(ResultPath, PChar(CurPath), FILE_ATTRIBUTE_DIRECTORY,
PChar(Dest), FILE_ATTRIBUTE_DIRECTORY) then
begin
Result := ResultPath;
if NoDot then
Result:=Copy(Result,3,Length(Result)-2);
end
else
Result := Dest;
end;
//得到文件列表
procedure GetFileList(const aDir,aFilter:string; const FileName:TFileName;AbSolutePath:Boolean=True;Recursion:Boolean=False);
{aDir:文件路径. aFilter:通配符(所有文件则*.*).FileName:输出文件名;AbSolutePath:使用绝对路径;Recursion:是否递归搜索}
var
aList:TStringList;
procedure AddFiles(TmpDir:string);
var
SR:TSearchRec;
Dir:string;
FileDir:string;
begin
Dir:=IncludeTrailingPathDelimiter(TmpDir);
if AbSolutePath then
FileDir:=Dir
else FileDir:=GetFileRelativePath(IncludeTrailingPathDelimiter(aDir),Dir,True);
if FindFirst(Dir+aFilter,faAnyFile,SR)=0 then
begin
repeat
if (SR.Name < > ". ") and (SR.Name < > ".. ") then
begin
if SR.Attr=faDirectory then
begin
if Recursion then
AddFiles(Dir+SR.Name);
end
else
aList.Add(FileDir+SR.Name+#9#9+
FormatDateTime( "yyyy-mm-dd hh:nn:ss ",FileTimeToDateTime(SR.FindData.ftLastWriteTime)));
end;
until FindNext(SR) < >0;
SysUtils.FindClose(SR);
end;
end;
begin
aList:=TStringList.Create;
try
AddFiles(aDir);
aList.SaveToFile(FileName);
finally
aList.Free;
end;
end;
//使用方法:
procedure TForm1.btn2Click(Sender: TObject);
begin
//文件保存在D:/abc.txt
GetFileList(Edit1.Text, "*.* ", "D:/abc.txt ",chkbx2.Checked,chkbx1.Checked);
end;
function FileTimeToDateTime(const AFileTime:TFileTime):TDateTime;
var
SYSTime:TSystemTime;
begin
if FileTimeToSystemTime(AFileTime,SYSTime) then
Result:=SystemTimeToDateTime(SYSTime)
else Result:=0;
end;
function PathRelativePathTo(pszPath:LPSTR; pszFrom:LPCSTR; dwAttrFrom:DWORD;
pszTo:LPCSTR; dwAttrTo:DWORD):BOOL; stdcall;
external "shlwapi.dll " name "PathRelativePathToA ";
//取得相对路径
function GetFileRelativePath(CurPath:string; const Dest:string; const NoDot:Boolean=False):string;
var
ResultPath: array[0..1024] of Char;//路径转换结果
begin
CurPath:=ExtractFileDir(CurPath)+ " ";
if PathRelativePathTo(ResultPath, PChar(CurPath), FILE_ATTRIBUTE_DIRECTORY,
PChar(Dest), FILE_ATTRIBUTE_DIRECTORY) then
begin
Result := ResultPath;
if NoDot then
Result:=Copy(Result,3,Length(Result)-2);
end
else
Result := Dest;
end;
//得到文件列表
procedure GetFileList(const aDir,aFilter:string; const FileName:TFileName;AbSolutePath:Boolean=True;Recursion:Boolean=False);
{aDir:文件路径. aFilter:通配符(所有文件则*.*).FileName:输出文件名;AbSolutePath:使用绝对路径;Recursion:是否递归搜索}
var
aList:TStringList;
procedure AddFiles(TmpDir:string);
var
SR:TSearchRec;
Dir:string;
FileDir:string;
begin
Dir:=IncludeTrailingPathDelimiter(TmpDir);
if AbSolutePath then
FileDir:=Dir
else FileDir:=GetFileRelativePath(IncludeTrailingPathDelimiter(aDir),Dir,True);
if FindFirst(Dir+aFilter,faAnyFile,SR)=0 then
begin
repeat
if (SR.Name < > ". ") and (SR.Name < > ".. ") then
begin
if SR.Attr=faDirectory then
begin
if Recursion then
AddFiles(Dir+SR.Name);
end
else
aList.Add(FileDir+SR.Name+#9#9+
FormatDateTime( "yyyy-mm-dd hh:nn:ss ",FileTimeToDateTime(SR.FindData.ftLastWriteTime)));
end;
until FindNext(SR) < >0;
SysUtils.FindClose(SR);
end;
end;
begin
aList:=TStringList.Create;
try
AddFiles(aDir);
aList.SaveToFile(FileName);
finally
aList.Free;
end;
end;
//使用方法:
procedure TForm1.btn2Click(Sender: TObject);
begin
//文件保存在D:/abc.txt
GetFileList(Edit1.Text, "*.* ", "D:/abc.txt ",chkbx2.Checked,chkbx1.Checked);
end;