找到一个(等下又有人所我=Copy + Paste 了) procedure findfilename(direc:string); var SearchRec: TSearchRec; list:tstringlist; retval:integer; begin list:=tstringlist.create; RetVal := FindFirst('c:\temp\*.*', faAnyFile, SearchRec); try while RetVal = 0 do begin if (searchrec.name<>'.') and (searchrec.name<>'..') then list.add(searchrec.name); RetVal := FindNext(SearchRec); end; finally FindClose(SearchRec); end; end;
//这样读取一个目录下的所有文件 uses ShellAPI; function DoOpenFile(sDirName:String;Flag:Boolean):Boolean; var hFindFile:Cardinal; F:Integer; //hFindFile:TSearchRec; tfile,FileName:String; sCurDir:String[255]; FindFileData:WIN32_FIND_DATA; CreateFT, LastAccessFT, LastWriteFT: TFileTime; ST: TSystemTime; ST1:TDateTime; begin sCurDir:=GetCurrentDir; ChDir(sDirName); hFindFile:=FindFirstFile('*.*',FindFileData); if hFindFile<>INVALID_HANDLE_VALUE then begin repeat tfile:=FindFileData.cFileName; if (tfile='.') or (tfile='..') then Continue; if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then begin if sDirName[Length(sDirName)]<>'\' then FileName:=sDirName+'\'+tfile else FileName:=sDirName+tfile; DoOpenFile(FileName,Flag);//第归 end else begin if Flag=False then begin F := CreateFile(Pchar(sDirName+'\'+tfile), GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if F=INVALID_HANDLE_VALUE then begin ShowMessage('Can not open file!'); Result:=False; Exit; end; //以下处理遍厉到的每个文件 if GetFileTime(F,@CreateFT, @LastAccessFT, @LastWriteFT) then begin FileTimeToSystemTime(LastWriteFT, ST); ST1:=StrToDate(IntToStr(ST.wYear)+'-'+IntToStr(ST.wMonth)+'-'+IntToStr(ST.wDay)); CloseHandle(F); if ST1-now >FilterDay then begin if MoveData(tfile,sDirName)=False then Continue; end else Continue end else begin Continue; CloseHandle(F); end; end else if MoveData(tfile,sDirName)=False then Continue; end; //处理完当前文件 until FindNextFile(hFindFile,FindFileData)=False; Windows.FindClose(hFindFile); end else begin ChDir(sCurDir); Result:=False; Exit; end; ChDir(sCurDir); Result:=True; end;
procedure findfilename(direc:string);
var SearchRec: TSearchRec;
list:tstringlist;
retval:integer;
begin
list:=tstringlist.create;
RetVal := FindFirst('c:\temp\*.*', faAnyFile, SearchRec);
try
while RetVal = 0 do
begin
if (searchrec.name<>'.') and (searchrec.name<>'..') then
list.add(searchrec.name);
RetVal := FindNext(SearchRec);
end;
finally
FindClose(SearchRec);
end;
end;
uses ShellAPI;
function DoOpenFile(sDirName:String;Flag:Boolean):Boolean;
var
hFindFile:Cardinal;
F:Integer;
//hFindFile:TSearchRec;
tfile,FileName:String;
sCurDir:String[255];
FindFileData:WIN32_FIND_DATA;
CreateFT, LastAccessFT, LastWriteFT: TFileTime;
ST: TSystemTime;
ST1:TDateTime;
begin
sCurDir:=GetCurrentDir;
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile<>INVALID_HANDLE_VALUE then
begin
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then
Continue;
if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then
begin
if sDirName[Length(sDirName)]<>'\' then
FileName:=sDirName+'\'+tfile
else
FileName:=sDirName+tfile;
DoOpenFile(FileName,Flag);//第归 end else
begin
if Flag=False then
begin
F := CreateFile(Pchar(sDirName+'\'+tfile), GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if F=INVALID_HANDLE_VALUE then
begin
ShowMessage('Can not open file!');
Result:=False;
Exit;
end;
//以下处理遍厉到的每个文件
if GetFileTime(F,@CreateFT, @LastAccessFT, @LastWriteFT) then
begin
FileTimeToSystemTime(LastWriteFT, ST); ST1:=StrToDate(IntToStr(ST.wYear)+'-'+IntToStr(ST.wMonth)+'-'+IntToStr(ST.wDay));
CloseHandle(F); if ST1-now >FilterDay then
begin
if MoveData(tfile,sDirName)=False then Continue;
end else
Continue
end else
begin
Continue;
CloseHandle(F);
end;
end else
if MoveData(tfile,sDirName)=False then Continue;
end;
//处理完当前文件
until FindNextFile(hFindFile,FindFileData)=False;
Windows.FindClose(hFindFile);
end else
begin
ChDir(sCurDir);
Result:=False;
Exit;
end;
ChDir(sCurDir);
Result:=True;
end;
用delphi自己的FileListBox1控件一下子就出来啊
用Delphi自己的FileListBox1控件可以把指定目录中的子目录中的文件显示出来吗??呵呵,得罪了..........