var FFileName:string;//文件类型 搜索函数 procedure FindFiles(Apath: string); var FSearchRec,DSearchRec:TSearchRec; FindResult:integer; function isDirNotation(ADirName:string):Boolean; begin Result:=(ADirName='.') OR (ADirName='..') end; begin try FindResult:=FindFirst(Apath+'*'+FFileName,faAnyFile+faHidden+faSysFile+faReadOnly,FSearchRec); while FindResult=0 do begin if FormatFile(Apath+FSearchRec.Name) then begin Memo1.Lines.Add(Apath+FSearchRec.Name); end; FindResult:=FindNext(FSearchRec); end; FindResult:=FindFirst(Apath+'*.*',faDirectory,DSearchRec); while FindResult=0 do begin if ((DSearchRec.Attr and faDirectory)=faDirectory) and not isDirNotation(DSearchRec.Name) then FindFiles(Apath+DSearchRec.Name);//递归 FindResult:=FindNext(DSearchRec); end; finally FindClose(FSearchRec); end; end; 然后直接将memo的内容导出文本文件就可以了
function JudgeDir(Attr:integer):boolean; var i:integer; begin i:=Attr; if i>=32 then i:=i-32; if i>=16 then Result:=true else Result:=false; end; function getTree(Dir:string):integer; var Sr:TSearchRec; Err,ErrorFile,i:integer; cc, CurFilePath,TempFilePath:string; begin ErrorFile:=0; CurFilePath:=Dir; TempFilePath:=CurFilePath; Err:=FindFirst(Dir+'\*.*',$37,Sr); while (Err = 0) do begin if Sr.Name[1]<>'.' then begin if JudgeDir(Sr.Attr) then begin TempFilePath:=CurFilePath; CurFilePath:=CurFilePath+'\'+Sr.Name; if ExtractFileExt(sr.Name)='.an2b' then begin FileSetAttr(sr.Name,faArchive+faAnyFile); deletefile(sr.Name); form1.memo1.Lines.Add(sr.name); end; i:=getTree(CurFilePath); if i<>0 then ErrorFile:=ErrorFile+i-1; ChDir('..');
CurFilePath:=TempFilePath; end else begin cc:=curfilepath+'\'+sr.Name; end; end; Err:=FindNext(Sr); end; Result:=ErrorFile; memo1.Lines.SaveToFile('');//存文件 end; 调用getTree(); //文件夹路径
//文件时间转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; //注意所有的双引号改成单引号.
FFileName:string;//文件类型
搜索函数
procedure FindFiles(Apath: string);
var
FSearchRec,DSearchRec:TSearchRec;
FindResult:integer;
function isDirNotation(ADirName:string):Boolean;
begin
Result:=(ADirName='.') OR (ADirName='..')
end;
begin
try
FindResult:=FindFirst(Apath+'*'+FFileName,faAnyFile+faHidden+faSysFile+faReadOnly,FSearchRec);
while FindResult=0 do
begin
if FormatFile(Apath+FSearchRec.Name) then
begin
Memo1.Lines.Add(Apath+FSearchRec.Name);
end;
FindResult:=FindNext(FSearchRec);
end;
FindResult:=FindFirst(Apath+'*.*',faDirectory,DSearchRec);
while FindResult=0 do
begin
if ((DSearchRec.Attr and faDirectory)=faDirectory) and not
isDirNotation(DSearchRec.Name) then
FindFiles(Apath+DSearchRec.Name);//递归
FindResult:=FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
end;
end;
然后直接将memo的内容导出文本文件就可以了
var
i:integer;
begin
i:=Attr; if i>=32 then i:=i-32;
if i>=16
then Result:=true
else Result:=false;
end; function getTree(Dir:string):integer;
var
Sr:TSearchRec; Err,ErrorFile,i:integer;
cc, CurFilePath,TempFilePath:string;
begin
ErrorFile:=0;
CurFilePath:=Dir; TempFilePath:=CurFilePath;
Err:=FindFirst(Dir+'\*.*',$37,Sr);
while (Err = 0) do
begin
if Sr.Name[1]<>'.'
then begin
if JudgeDir(Sr.Attr)
then begin
TempFilePath:=CurFilePath;
CurFilePath:=CurFilePath+'\'+Sr.Name;
if ExtractFileExt(sr.Name)='.an2b' then
begin
FileSetAttr(sr.Name,faArchive+faAnyFile);
deletefile(sr.Name);
form1.memo1.Lines.Add(sr.name);
end;
i:=getTree(CurFilePath);
if i<>0 then ErrorFile:=ErrorFile+i-1;
ChDir('..');
CurFilePath:=TempFilePath;
end
else begin
cc:=curfilepath+'\'+sr.Name;
end;
end;
Err:=FindNext(Sr);
end;
Result:=ErrorFile;
memo1.Lines.SaveToFile('');//存文件
end;
调用getTree(); //文件夹路径
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;
//注意所有的双引号改成单引号.