遍历,过滤,拷贝,高手请进!急!!在线等!!!

laohai 2003-10-16 03:57:11
在指定的D盘或任意文件夹下,(例如C:\或C:\sheji) ,只获得所有的*.txt文件,并将其拷贝到一个指定的目录下
(例如D:\shiyan)下,怎么办,有代码的最好,急用,谢谢
...全文
62 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
huojiehai 2003-10-16
  • 打赏
  • 举报
回复
拷目录这有公共函数,你再加过滤就是你要的了
http://haitian.myrice.com/Soft/MyPubFuncUnit.Txt
hmily1688 2003-10-16
  • 打赏
  • 举报
回复
用楼上的不过不会拷贝sheji目录下的子目录的文件,还出现一个dos窗体
delphi_xizhousheng 2003-10-16
  • 打赏
  • 举报
回复
uses shellapi



shellExecute(handle,'Open','xcopy','C:\sheji\*.txt D:\shiyan',nil, SW_SHOWNORMAL );
laohai 2003-10-16
  • 打赏
  • 举报
回复

我自己顶一下
liufuyahong 2003-10-16
  • 打赏
  • 举报
回复
procedure TForm1.FindFile(strDir,strName:string);
var
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then
FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then
FileAttrs := FileAttrs + faAnyFile;

if FindFirst(strDir+strName, FileAttrs, sr) = 0 then
begin
repeat//find the file
if (sr.Attr and FileAttrs) = sr.Attr then
begin
CopyFile(pchar(sr.Name),pchar('D:\shiyan\'+sr.Name),true);//Copy the file do the dest dir
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
FindFile('D:\Program Files\Borland\Delphi7\Projects\','*.Txt');
end;
Delphi_Li 2003-10-16
  • 打赏
  • 举报
回复
以下代码及供参考,临时写的,没有测试!!!

Bool Function CopyFileEx( Dir : String );
Var
Rec : TSearchRec;
Done : Integer;
Begin
Done = FindFirst( Dir + '\*.*', faAnyFile, Rec );
While( Done = 0 ) Do
Begin
if( UpperCase( ExtractFileExt( Rec.Name ) ) = '.TXT' ) Then
Begin
CopyFile( Dir + '\' + Rec.Name, 'D:\ShiYan', False );
End;
if( Rec.Attr = faDirectory ) Then
Begin
CopyFileEx( Dir + '\' + Rec.Name );
End;
Done = FineNext( Rec );
End;
FindClose( Rec );
Result := False;
End;

大概就是这样了!!!
IORILI 2003-10-16
  • 打赏
  • 举报
回复
1. 从搜索记录中判断是否是子目录。

 

function IsValidDir(SearchRec:TSearchRec):Boolean;

begin


if (SearchRec.Attr=16) and

(SearchRec.Name<>'.') and

(SearchRec.Name<>'..') then

Result:=True

else

Result:=False;

end;

2. 这是查询主体函数。

参数介绍:

Mainpath: 指定的查询目录。

Filename: 欲查询的文件。

Foundresult: 返回的含完整路径的匹配文件(可能有多个)。

如果有匹配文件,函数返回True,否则,返回False;

 

function SearchFile(mainpath:string;filename:string;

var foundresult:TStrings):Boolean;

var

i:integer;

Found:Boolean;

subdir1:TStrings;

searchRec:TsearchRec;

begin

found:=false;

if Trim(filename)<>'' then

begin

subdir1:=TStringList.Create;//字符串列表必须动态生成

//找出所有下级子目录。

if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then

begin

if IsValidDir(SearchRec) then

subdir1.Add(SearchRec.Name);

while (FindNext(SearchRec) = 0) do

begin

if IsValidDir(SearchRec) then

subdir1.Add(SearchRec.Name);

end;

end;

FindClose(SearchRec);

//查找当前目录。

if FileExists(mainpath+filename) then

begin

found:=true;

foundresult.Add(mainpath+filename);

end;

//这是递归部分,查找各子目录。

for i:=0 to subdir1.Count-1 do

found:=Searchfile(mainpath+subdir1.Strings[i]+

'\',Filename,foundresult)or found;

//资源释放并返回结果。

subdir1.Free;

end;

result:=found;

end;

copyfile拷贝
hansome 2003-10-16
  • 打赏
  • 举报
回复
用FindFirst、FindNext
遍历所有目录和文件
然后通过ExtractFileExt提取文件的扩展名
最后用CopyFile拷贝文件

以上函数均为API函数

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧