Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-10-2008, 04:14 AM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 202
Default Recursive file search (Subfolders only)

Hi All, I am using a modified version of :

http://www.delphipages.com/threads/thread.cfm?ID=176043&G=176042

however I need to list only files in subfolders for example

Code:
mainfolder
|
|-pictures
    |
    |-a
    |-b
    |-c
      |-t
if I specify 'mainfolder\pictures' i need to get a list of all files within a,b,c,t etc not in 'pictures'

how can I specify this with a findfirst loop?

------------------------------------
If you look like your passport photo
you are too ill to travel
Reply With Quote
  #2  
Old 02-10-2008, 04:21 AM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 202
Default RE: Recursive file search (Subfolders only)

Or if anyone has a routine to move all files in subfolders to the specified folder.

------------------------------------
If you look like your passport photo
you are too ill to travel
Reply With Quote
  #3  
Old 02-10-2008, 04:48 AM
TheCaptain TheCaptain is offline
Senior Member
 
Join Date: Dec 2005
Posts: 194
Default RE: Recursive file search (Subfolders only)

This should get you pretty close.




Code:
procedure TForm1.RecurseFolders(szCurrentFolder: String);
var
  sr            : TSearchRec;
  iResult       : Integer;
  szNextFolder  : String;
begin

  iResult := FindFirst(szCurrentFolder + '\*.*',faAnyFile,sr);
  while iResult = 0 do
  begin
    if sr.Attr = faDirectory then
    begin
      if (sr.Name <> '.') and (sr.Name <> '..') then
      begin
        ListBox1.Items.Add(szIndent + szCurrentFolder + sr.Name);
        szNextFolder := szCurrentFolder + '\' + sr.Name;
        szIndent := szIndent + '   ';
        RecurseFolders(szNextFolder);
      end;
    end else
      ListBox1.Items.Add(szIndent + szCurrentFolder + sr.Name);
    iResult := FindNext(sr);
  end;
  if Length(szIndent) > 0 then
    szIndent := Copy(szIndent,1,Length(szIndent) - 3);
end;
The Captain

Reply With Quote
  #4  
Old 02-10-2008, 05:41 AM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 202
Default RE: Recursive file search (Subfolders only)

That give a similar result as I have already, How would I modify it so that the list does not include the folder that is passed to the procedure?

in my original post, if I called :
Code:
RecurseFolders('pictures');
I do not want any files in 'pictures' listed, just the sub folders of 'pictures'

------------------------------------
If you look like your passport photo
you are too ill to travel
Reply With Quote
  #5  
Old 02-10-2008, 06:01 AM
TheCaptain TheCaptain is offline
Senior Member
 
Join Date: Dec 2005
Posts: 194
Default RE: Recursive file search (Subfolders only)

Call the procedure with the starting folder - RecurseFolders('Mainform\pictures\');. This will only display folders lower than \pictures\.

The Captain

Reply With Quote
  #6  
Old 02-10-2008, 06:09 AM
Theo Theo is offline
Junior Member
 
Join Date: Jul 2004
Posts: 27
Default RE: Recursive file search (Subfolders only)

Perhaps you should use a levelcount (gbl) and skip level 1 like:

procedure GetFilesX(StartDirX: string);
var LvlCnt : word;
procedure GetFiles(StartDir: string);
var
Search : TSearchRec;
begin
Inc(LvlCnt);
StartDir:=IncludeTrailingBackslash(StartDir);
if FindFirst(startdir + '*.*', faAnyFile, Search) = 0 then
repeat
if (Search.Name[1] <> '.' ) then
if ((Search.Attr and faDirectory) > 0) then
begin
GetFiles(StartDir + Search.Name);
end else
begin
If LvlCnt>1 then (StartDir + Search.Name)
Application.ProcessMessages;
end;
until FindNext(Search) <> 0;
FindClose(Search);
Dec(LvlCnt);end;

begin {GetFilesX }
LvlCnt := 0;
GetFiles(StartDirX);
end;
Reply With Quote
  #7  
Old 02-10-2008, 06:11 AM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 202
Default RE: Recursive file search (Subfolders only)

I have tried that, I have the structure from my original post setup on my 'Z' drive with a txt file in each one named after the folder, and I get the following

Code:
Z:\MainForm\pictures\a
Z:\MainForm\pictures\a\a.txt
   Z:\MainForm\pictures\b
Z:\MainForm\pictures\b\a.txt
      Z:\MainForm\pictures\c
Z:\MainForm\pictures\c\c.txt
Z:\MainForm\pictures\c\t
Z:\MainForm\pictures\c\t\t.txt
         Z:\MainForm\pictures\pictures.txt
I still have the pictures folder listed when I passed

ResuceFolders('z:\mainform\pictures')

------------------------------------
If you look like your passport photo
you are too ill to travel
Reply With Quote
  #8  
Old 02-10-2008, 07:03 AM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 202
Default RE: Recursive file search (Subfolders only)

Thanks Theo, Just the thing i needed

------------------------------------
If you look like your passport photo
you are too ill to travel
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:46 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.