您的位置:程序门 -> delphi -> 语言基础/算法/系统设计



如何在硬盘上查找文件


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何在硬盘上查找文件
发表于:2007-11-28 23:26:31 楼主
我在硬盘中查找一个文件,不知道在哪个分区中    
    用什么函数可以返回路径的    
    比如:我查找一个gj.exe    
    后结果返回c:\gj\    
     
     
    相当于windows的搜索,返回要找文件的路径(是整个硬盘的搜索)    
    大家有函数或自己些过的,可否给小弟一个参考?》急啊!!!    
发表于:2007-11-29 08:38:391楼 得分:0
我也找了几天都没有什么好的方法,,,顶,,关注,,
发表于:2007-11-29 08:59:332楼 得分:0
顶,,
发表于:2007-11-29 08:59:463楼 得分:0
顶,,
发表于:2007-11-29 09:36:054楼 得分:0
我记得有个api叫   findfile..什么的,   可以用来搜索文件的.
发表于:2007-11-29 09:50:215楼 得分:0
顶,,
发表于:2007-11-29 09:50:346楼 得分:0
顶,,
发表于:2007-11-29 09:51:587楼 得分:0
d区的高手都到那里去了,是闲分少吗?如果谁能回答出问题,我另外开贴加分,,,
发表于:2007-11-29 13:28:038楼 得分:0
那函数叫
查找目录中是否存在某一文件
function   filesearch(const   name,   dirlist:   string):   string;
具体用法,请查看相关帮助. 输入这函数名字,delphi会有提示的吧
发表于:2007-11-29 14:35:599楼 得分:0
想对需要的结果而言,有一些区别,但是有总比没有强,,谢谢,,
发表于:2007-11-29 14:54:2710楼 得分:0
还有这个和filesearch差不多,
      function   findfirst(const   path:   string;   attr:   integer;   var   f:   tsearchrec):   integer;
你要的结果好像只能遍历去了!一个函数可搞不定。

发表于:2007-11-29 16:09:1111楼 得分:0
用递归调用啊先用findfirst寻找第一个文件,再用findnext寻找下一个文件,如果是目录那么就递归.
这些不应该问,随便来来就会了
发表于:2007-11-29 22:19:2312楼 得分:0
function   tfrmmain.findfiles(apath:   string):   integer;
var
    searchrec:tsearchrec;
    fileattrs:   integer;
    filedriverdatetime,filesavedatetime:string;
    tempfile:string;
begin
    result:=0;
    fileattrs:=faanyfile;
    if   findfirst(apath+'\*.*',fileattrs,searchrec)   =   0   then
    begin
        try
            if(searchrec.name <> '.')   and   (searchrec.name <> '..')   then
            begin
                if   searchrec.attr   =   fadirectory   then     //目录继续搜索,递归
                      result:=result+findfiles(apath+'\'+searchrec.name)
                else   begin                                                           //是文件则进行转发
                      try
                          result:=result+1;
                          tempfile:=apath+'\'+searchrec.name;
                          memo1.lines.add(tempfile);
                          filedriverdatetime:=getfiledriverdatetime(tempfile);
                          filesavedatetime:=getfilesavedatetime(tempfile);
                          if   morefilepathtransmitfile(tempfile,'',filedriverdatetime,filesavedatetime)   then
                                ;
                                deletefile(tempfile);
                      finally
                      end;
                end;
            end;
            while   findnext(searchrec)=0   do
            begin
                if(searchrec.name <> '.')   and   (searchrec.name <> '..')   then
                begin
                    if   searchrec.attr   =   fadirectory   then   //目录继续搜索,递归
                        result:=result+findfiles(apath+'\'+searchrec.name)
                else   begin                                                           //是文件则进行转发
                      try
                          result:=result+1;
                          tempfile:=apath+'\'+searchrec.name;
                          memo1.lines.add(tempfile);
                          filedriverdatetime:=getfiledriverdatetime(tempfile);
                          filesavedatetime:=getfilesavedatetime(tempfile);
                          if   morefilepathtransmitfile(tempfile,'',filedriverdatetime,filesavedatetime)   then
                                ;
                                deletefile(tempfile);
                      finally
                      end;
                end;
                end;
            end;
        finally
            findclose(searchrec);
        end;
    end;
end;
把我的代码去掉就是了。
发表于:2007-11-29 22:25:4913楼 得分:0
不推荐delphi自己那个findfirst
推荐win32api的
findfirstfile
findnextfile
支持64位文件大小的(largeinteger)
发表于:2007-11-30 09:08:1914楼 得分:0
谢谢各位了.问题已经解决了.用的是以下方法:


procedure   tfdug_findfile.findfiles(apath,   afile:   string;     filelist:   tstrings);
var
  findresult:   integer;
  fsearchrec,   dsearchrec:   tsearchrec;
  function   isdirnotation(adirname:   string):   boolean;
  begin
      result   :=   ((adirname   =   '.')   or   (adirname   =   '..'));
  end;
begin
  if   apath[length(apath)]   <>   '\'   then
      apath   :=   apath   +   '\';
  findresult   :=   findfirst(apath   +   afile,   faanyfile   +
                                                  fahidden   +fasysfile   +   fareadonly,
                                                  fsearchrec);   //在根目录中查找指定文件
  try
      while   findresult   =   0   do
      begin
          filelist.add(apath   +   fsearchrec.name);
          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函数
              findfiles(apath   +   dsearchrec.name,   afile,filelist);
          findresult   :=   findnext(dsearchrec);
      end;

  finally
      findclose(fsearchrec);
  end;

end;


发表于:2007-11-30 09:27:4415楼 得分:0
来晚了...

恭喜楼主,问题得以解决
发表于:2007-12-04 09:09:2216楼 得分:0
结贴了.


快速检索

最新资讯
热门点击