遍历目录的源码求教

解决方案 »

  1.   

    可能对你有帮助:
    ---- 1.1拷贝目录的递归辅助函数:DoCopyDir function DoCopyDir(sDirName:String;
    sToDirName:String):Boolean;
    var
       hFindFile:Cardinal;
       t,tfile:String;
       sCurDir:String[255];
       FindFileData:WIN32_FIND_DATA;
    begin
       //先保存当前目录
       sCurDir:=GetCurrentDir;
       ChDir(sDirName);
       hFindFile:=FindFirstFile('*.*',FindFileData);
       if hFindFile< >INVALID_HANDLE_VALUE then
       begin
            if not DirectoryExists(sToDirName) then
               ForceDirectories(sToDirName);
            repeat
                  tfile:=FindFileData.cFileName;
                  if (tfile='.') or (tfile='..') then
                     Continue;
                  if FindFileData.dwFileAttributes=
                  FILE_ATTRIBUTE_DIRECTORY then
                  begin
                       t:=sToDirName+'\'+tfile;
                       if  not DirectoryExists(t) then
                           ForceDirectories(t);
                       if sDirName[Length(sDirName)]< >'\' then
                          DoCopyDir(sDirName+'\'+tfile,t)
                       else
                          DoCopyDir(sDirName+tfile,sToDirName+tfile);
                  end
                  else
                  begin
                       t:=sToDirName+'\'+tFile;
                       CopyFile(PChar(tfile),PChar(t),True);
                  end;
            until FindNextFile(hFindFile,FindFileData)=false;
            FindClose(hFindFile);
       end
       else
       begin
            ChDir(sCurDir);
            result:=false;
            exit;
       end;
       //回到原来的目录下
       ChDir(sCurDir);
       result:=true;
    end;
      

  2.   

    下面这个过程是遍历定位目录树的,其中第一个参数指定目录树,第二个参数指定要定位的节点的文本procedure ergodetree(treeview1:ttreeview;str:string);//遍历定位目录树
    var
     i : integer;
    begin
      treeview1.Selected:=treeview1.Items.Item[0];
    for i :=1 to treeview1.Items.Count do
      begin
        treeview1.SetFocus;
    //    showmessage(TreeView1.Selected.Text);
    if treeview1.Selected.getFirstChild = nil then
       treeview1.Selected:=treeview1.Selected.GetNext
       else
       treeview1.Selected:=treeview1.Selected.getFirstChild;
       if (treeview1.Selected.GetNext = nil) and (treeview1.Selected.Parent <> treeview1.TopItem) then
         treeview1.Selected:=treeview1.Selected.Parent.GetNext;
           if (treeview1.Selected.GetNext = nil) and (treeview1.Selected.Parent = treeview1.TopItem) then
             exit; if treeview1.Selected.Text = str then
       exit;
       end;
    end;
      

  3.   

    看看这个procedure TForm1.Button1Click(Sender: TObject);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;  with StringGrid1 do
      begin
        RowCount := 0;    if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then    begin
          repeat
            if (sr.Attr and FileAttrs) = sr.Attr then
            begin
            RowCount := RowCount + 1;
            Cells[1,RowCount-1] := sr.Name;
            Cells[2,RowCount-1] := IntToStr(sr.Size);
            end;
          until FindNext(sr) <> 0;
          FindClose(sr);
        end;
      end;
    end;
      

  4.   

    请问有没有用findfirst,findnext的例子?
      

  5.   

    楼主,我最后发的不就是findfirst和findnext的例子吗?不行就去看帮助去
      

  6.   

    我看了这个例子的,findfirst的第一个参数是指的根目录么?就是在edit1.text这个目录下搜索第一个符合fileattrs属性的文件,并且返回到sr中,是这个意思?
      

  7.   

    看我的!简单明了,一个函数搞定...procedure TCryptForm.SearchFile(Path: string; Mask: string;
    SubFolder: boolean; FoundResult: TStrings; var Num: integer);
    var
    i, Count: integer;
    SubDir: TStrings;
    SearchRec: TSearchRec;
      {Begin Local Function}
      function IsValidDir(SearchRec: TSearchRec): integer;
      begin
    if (SearchRec.Attr <> 16) and  (SearchRec.Name <> '.') and
         (SearchRec.Name <> '..') then
    Result := 0 {It's not dir}
    else
    if (SearchRec.Attr = 16) and  (SearchRec.Name <> '.') and
           (SearchRec.Name <> '..') then
    Result := 1 {It's not root dir}
         else Result := 2; {It's root dir}
      end;
      {End Local Function}
    begin
    if Trim(Mask) <> '' then
      begin
    if (FindFirst(Path + Mask, faAnyFile, SearchRec) = 0) then
        begin
    repeat
    if IsValidDir(SearchRec) = 0 then
            begin
    Inc(Num);
    FoundResult.Add(Path + SearchRec.Name);
            end;
    Application.ProcessMessages;
    until (FindNext(SearchRec) <> 0);
        end;
        FindClose(SearchRec);
        if SubFolder then
        begin
          SubDir := TStringList.Create;
          if (FindFirst(Path + '*.*', faDirectory, SearchRec) = 0) then
          begin
            repeat
              if IsValidDir(SearchRec) = 1 then SubDir.Add(SearchRec.Name);
              Application.ProcessMessages;
            until (FindNext(SearchRec) <> 0);
            end;
          FindClose(SearchRec);
          Count := SubDir.Count - 1;
          for i := 0 to Count do
            SearchFile(Path + SubDir.Strings[i] + '\', Mask, true, FoundResult, Num);
          FreeAndNil(SubDir);
        end;
      end;
    end;
      

  8.   

    kangpig(小康猪):你理解的对,自己动手试试不就明白了?