Solved

Find First / Next Unstable ??

Posted on 2002-05-12
3
180 Views
Last Modified: 2010-04-04
hello everyone!!

when i use find first/next to find directory,
why if i change directory-property (hidden or readonly or is checked), the result is unstable..

but if i add fileattrs with fahidden or fareadonly,
the other directory sometime not show.. :(


here is my program to check find first/next :

// function to sort my treeview..
function CustomSortProc(Node1, Node2: TTreeNode; Data: Longint): Integer; stdcall;
begin
     if Data = 0 then
        Result := AnsiStrIComp(Pchar(Node1.Text), PChar(Node2.Text))
     else
         Result := -AnsiStrIComp(PChar(Node1.Text), PChar(Node2.Text));
end;

procedure tform1.finddirectory(fileattrs : integer);
var
   sr: TSearchRec;
   i : Integer;
   plus : string;
begin
     i := 0;
     if directorylistbox1.directory[length(directorylistbox1.directory)] = '\' then
        plus := '*.*'
     else
         plus := '\*.*';

     if FindFirst(directorylistbox1.Directory + plus, FileAttrs, sr) = 0 then
     begin
          if (sr.Attr and FileAttrs) = sr.Attr then
          begin
               if not ((sr.name = '.') or (sr.name = '..')) then
               begin
                    inc(i);
                    TreeView1.Items.Addchild(nil, SR.Name);
               end;
          end;
          while FindNext(sr) = 0 do
          begin
               if (sr.Attr xor FileAttrs) = 0 then
               begin
                    if not ((sr.name = '.') or (sr.name = '..')) then
                    begin
                         inc(i);
                         TreeView1.Items.Addchild(nil, SR.Name);
                    end;
               end;
          end;
          FindClose(sr);
     end;
     Treeview1.CustomSort(@CustomSortProc, 0);
     label1.caption := inttostr(i) + ' directories';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     treeview1.Items.Clear;
     finddirectory(fadirectory);                      // 1. show windows, folder1, folder2
     finddirectory(fadirectory+fahidden);             // 2. show mydocuments
     finddirectory(fadirectory+fareadonly);           // 3. show songs & windows
     finddirectory(fadirectory+fahidden+fareadonly);  // 4. show programfiles
end;


my directory in C:\ --> mydocuments, programfiles, songs, windows, folder1, folder2.

property of :
  mydocuments (hidden),
  programfiles (hidden+readonly),
  songs (readonly).

if i use the above method (1-4), the windows directory show double, and if i change to other drive, sometimes there is one directory shows double/triple.. (always happen on a root directory)

why ???  is there something wrong in my program ??
ps - i'm using d5 and windows 98 se
0
Comment
Question by:boyfn78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
AvonWyss earned 55 total points
ID: 7004833
Your loop is not really consistent. One time, you do this comparison:

         if (sr.Attr and FileAttrs) = sr.Attr then

but the other time, you do this:

         if (sr.Attr xor FileAttrs) = 0 then

These are not the same! Also note that the second is identical to

         if sr.Attr=FileAttrs then

which I assume you want to use in your case. The first one will also return results with more flags. So that's where your "doubles" come from. Rather use a loop like this:

     if FindFirst(directorylistbox1.Directory + plus, FileAttrs, sr) = 0 then try
          repeat
               if (sr.name<>'.') and (sr.name<>'..') and (sr.Attr=FileAttrs) then
               begin
                    inc(i);
                    TreeView1.Items.Addchild(nil, SR.Name);
               end;
              end;
          until FindNext(sr)<>0;
     finally
          FindClose(sr);
     end;

(This code replaces everything from FindFirst to FindClose in your code.)
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 7004834
Oops. just saw that there is one "end" too much in my code. Just remove it.
0
 

Author Comment

by:boyfn78
ID: 7006544

thank's AvonWyss..

you're right, and thank's 2x for your solution.. :p
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question