Solved

Find First / Next Unstable ??

Posted on 2002-05-12
3
175 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
Comment Utility
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
Comment Utility
Oops. just saw that there is one "end" too much in my code. Just remove it.
0
 

Author Comment

by:boyfn78
Comment Utility

thank's AvonWyss..

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

7 Experts available now in Live!

Get 1:1 Help Now