我想对一个目录下的文件都进行同样的操作,但是这些文件每次都不同。如何做到遍历一个目录下的所有文件?
不包含子目录应该怎么做,包含子目录又应该怎么做?
不包含子目录应该怎么做,包含子目录又应该怎么做?
解决方案 »
- 有工程目录下有.dcu文件没有.pas文件,编译时报错File not found:'...\xxx.pas',这是为什么?
- Delphi网络接口和串口编程
- 哪位大侠知道SReport这个控件的下载地址??
- 有哪位大哥做过卸载程序的来指教一下UP有分
- 欢迎指教
- 硬盘分区找不到了,里面有重要数据,怎么办高手帮忙。。。。
- 各位,紧急求救,怎么用DecisionQuery连接sql服务器!!SoS!!
- 一个小问题
- 该如何学习Delphi?
- 求救,我的delphi5缺少vclx50.bpl系列 ,有谁肯发一份,重谢,[email protected]
- 关于在ListView控件中显示位图文件的问题?
- 我的个**呀, 现在DELPHI怎么这么冷清了!!
var
f:TSearchRec;
begin
if FindFirst('*.*',$0000003F,f)=0 then
begin
//do something here
while FindNext(f) = 0 do
begin
//do somethine here
end;
FindClose(f);
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 findall(path:string;findsubdir:boolean;var fileresult:TStrings);
var
iCnt:integer;
sr:TSearchRec;
begin
iCnt:=FindFirst(path+'\*.*',faAnyFile,sr);
while iCnt=0 do
begin
if sr.name[1]<>'.' then//掠过'.' '..'
if (sr.attr and faDirectory)=faDirectory then//目录
begin
if findsubdir then
findall(path+'\'+sr.name,findsubdir,fileresult);//递归找子目录
end
else
fileresult.add(path+'\'+sr.name);//增加文件
iCnt:=FindNext(sr);
end;
FindClose(sr);
end;
procedure button1click(Sender:TObject);
var
tmp:TStings;
begin
tmp:=TStringList.create;
findall('C:\test',false,tmp);//找C:\test下的除目录文件以外的文件,且不的子目录
memo1.lines.assign(tmp);
tmp.free;
end;
也可以