?
Solved

Find First / Next Unstable ??

Posted on 2002-05-12
3
Medium Priority
?
182 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 220 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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