Solved

Find First / Next Unstable ??

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 317
Simple Delphi Question 9 96
Dev Express grid collapse 2 45
select query - oracle 16 100
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 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