Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recursive function missing folders 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
So in Thread102-1193165 everyone helped me with some code to do a recursive file search and now I'm re-using that code and it's not working correctly.

I have the following directory structure:

[tt]
CaseDocs
00
CR
0338400 (documents in these folders)
0408400
0434300
0524800
0565800
0589600
[/tt]

I am using the following code to go through all the directories and copy the documents in the folders to a specific location. However, I am only getting the documents from every OTHER folder. I got the documents from the bolded folders above. What am I missing that it's skipping a folder?

Thanks!

Leslie

Code:
procedure TForm1.Button2Click(Sender: TObject);
begin
  SweepDir('\\srvcasedocs\CaseDocs\00\CR\');
end;



procedure TForm1.SweepDir(Dir : AnsiString);
var
  SR  : TSearchRec;
begin
  if FindFirst(Dir + '*.*', faAnyFile, SR) = 0 then
  begin
  repeat
      if (SR.Name <> '.') and (SR.Name <> '..') then
        if SR.Attr = faDirectory then
        begin
          SweepDir(Dir + SR.Name + '\');
        end
        else
        begin          
          CopyFileTo(Dir + SR.Name, '\\srvimaging\imaging\Citation\citationimages\_Consol Test\' + SR.Name)
        end;
        FindNext(SR);
    until FindNext(SR) <> 0; //or FClosed;
    FindClose(SR);
  end;
end;

Leslie

In an open world there's no need for windows and gates
 
You do FindNext twice!

Code:
procedure TForm1.SweepDir(Dir : AnsiString);
var
  SR  : TSearchRec;
begin
  if FindFirst(Dir + '*.*', faAnyFile, SR) = 0 then
  begin
  repeat
      if (SR.Name <> '.') and (SR.Name <> '..') then
        if SR.Attr = faDirectory then
        begin
          SweepDir(Dir + SR.Name + '\');
        end
        else
        begin          
          CopyFileTo(Dir + SR.Name, '\\srvimaging\imaging\Citation\citationimages\_Consol Test\' + SR.Name)
        end;
        //FindNext(SR); not needed
    until FindNext(SR) <> 0; //or FClosed;
    FindClose(SR);
  end;
end;

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
always helps to have a second set of eyes!!!

thanks!

les
 
Here's another small problem in what you posted. Maybe it's not a problem for what you are doing, but:

Code:
  if SR.Attr = faDirectory then

This only picks up directories that have no other attributes in them but faDirectory. As I'm sure you know, other attributes (read-only/hidden/system) can be attached to directories. We see this a little more in the modern Windows systems.

Code:
if SR.Attr and faDirectory = faDirectory then

will produce something closer to the intention of such code in all instances.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top