自己的遍历某目录下文件
procedure TForm1.Button1Click(Sender: TObject);
var
data: WIN32_FIND_DATA ;
fhandle: cardinal;
ret:bool;
begin
fhandle:= FindFirstFile('c://temp//test//*.*',data);
if (handle= INVALID_HANDLE_VALUE) then
begin
MessageDlg('run error', mtConfirmation,[mbOK] , 0);
exit;
end;
ret:=true;
while(ret) do
begin
if((data.dwFileAttributes <>FILE_ATTRIBUTE_DIRECTORY) and
(data.dwFileAttributes <>FILE_ATTRIBUTE_READONLY) and
(data.dwFileAttributes <>FILE_ATTRIBUTE_HIDDEN) and
(data.dwFileAttributes <>FILE_ATTRIBUTE_SYSTEM)) then
begin
MessageDlg(data.cFileName, mtConfirmation,[mbOK] , 0);
end;
ret:=FindNextFile(fhandle,data);
end;
windows.FindClose(fhandle);
收藏的别人的findfirstfile
procedure TForm1.SearchFile(beginPath: PChar; NowList: TStringList);
var
searchrec:_WIN32_FIND_DATAA;
SearchHandle:cardinal;
searching:LongBool;
NowPath,AddPath:Pchar;
begin
GetMem(Nowpath,255);
strcopy(NowPath,Beginpath);
StrCat(NowPath,'*.*');
SearchHandle:=findfirstfile(NowPath,searchrec);
searching:=false;
if SearchHandle<>INVALID_HANDLE_VALUE then begin
searching:=true;
end;
While searching do begin
if SearchREc.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then begin
if (string(SearchRec.cFileName)<>'.') and (string(SearchRec.cFileName)<>'..' ) then begin
getmem(AddPath,255);
strcopy(addpath,beginpath);
strcat(addpath,searchRec.cFileName);
strcat(addpath,'/');
searchfile(addpath,NowList);
freemem(addpath);
end;
end else begin
getmem(AddPath,255);
strcopy(addpath,beginpath);
strcat(addpath,searchRec.cFileName);
NowList.Add(string(addpath));
freemem(addpath);
end;
searching:=findnextfile(SearchHandle,Searchrec);
end;
windows.findclose(Searchhandle);
FreeMem(Nowpath);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyList:TStringList;
i:integer;
begin
try
MyList:=TStringList.Create ;
//注意在此,应该确保调用参数的正确,没有检测哦
SearchFile('c:/chenhu2/',MyList);
for i:=0 to Mylist.Count -1 do begin
Listbox1.Items.Add(MyList[i]);
end;
finally
MyList.Free ;
end;
end;