Solved

Find First / Next Unstable ??

Posted on 2002-05-12
3
177 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now