I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyPascal and Delphi Coding BoardDelphi Single User Apps Topic   Print This     

Can anybody help me with getti...

Can anybody help me with getti... in Delphi Single User Apps topic (part of our Pascal and Delphi Coding group).

Quick Search: anybody   Can anybody   Can anybody me   getti   
owen.moore
Ashburton, -- NZ

Can anybody help me with getting a directory file listing into a listbox so that I can programatticlly go through the listbox and put the filenames into a database

they Database part is not causing me any trouble

I can get it into  a dirlistbox but can not use that to then post certain of the file names to the Database

any help is most appreciated

Owen

 Posted 9 years ago (Thread Starter)
Comment Quote
About owen.moore -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.

Post ID #14146, 4 replies
Thread Started 6/7/2010 1:14:20 AM
View Counter=3974
Last Reply Posted 6/10/2010 8:02:10 AM)
Location=Ashburton, -- NZ 
Joined=16 years ago   MB Posts=7  
Wes Peterson
Prestwood IT
Prestwood IT office in Citrus Heights, CA

Hi Owen.

This is pretty easy in Delphi and you don't really need the listbox unless you need to actually see the directories and/or files.

In Delphi Help, do a search for FindFirst and FindNext.  There's some good example code in there, too.

If you need to "drill down" into folders, you'll want to write a routine that can call itself recursively.

Wes Peterson
Senior Software Engineer
Prestwood IT Solutions
http://www.prestwood.com

 Posted 9 years ago
Comment Quote
About Wes Peterson -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
About Wes Peterson

Wes Peterson is a Senior Programmer Analyst with Prestwood IT Solutions where he develops custom Windows software and custom websites using .Net and Delphi. When Wes is not coding for clients, he participates in this online community. Prior to his 10-year love-affair with Delphi, he worked with several other tools and databases. Currently he specializes in VS.Net using C# and VB.Net. To Wes, the .NET revolution is as exciting as the birth of Delphi.


Post ID #14150 (Level 1.1)  Reply to 14146
Thread Started 6/7/2010 11:19:52 AM
Location=Prestwood IT office in Citrus Heights, CA 
Joined=15 years ago   MB Posts=158   KB Posts=163   KB Comments=34   BLOG, Topics=20  
owen.moore
Ashburton, -- NZ

Laughing Thanks to all that replied I got some good ideas and now have it all working nicely

This is a great forum and its good that you all offer your help to us

Cheers and thanks again

Owen

 Posted 9 years ago (Thread Starter)
Comment Quote
About owen.moore -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.

Post ID #14162 (Level 2.1)  Reply to 14150 and Parent is 14146
Thread Started 6/10/2010 3:02:03 AM
Location=Ashburton, -- NZ 
Joined=16 years ago   MB Posts=7  
Larry Rutledge
-- USA

Wes is correct there is no need to use a list box, and FindFirst()/FindNext() are the right constructs to use. A word of caution, be sure you use FindClose() to end the construct or you will leave resources hanging (I managed to bring an entire corporate network to its knees by forgetting this one simple command in my recursive network search many years ago).

If you want to build a list (e.g. Listbox) to hold the filename contents prior to inserting into the database you can use a TStringList (the object inside the list box which holds the strings). But if you just want to read a directory and write the names directly to a database then you can forgo the lists completely.

Help is definitely your friend for constructs like these and often give great examples. But just to augment, here's a quick snippet that might get you on your way:

...

var

  SR: TSearchRec;

  sPath: string;

  isFound: Boolean;

begin

  sPath := 'C:\test\'; // specify path to examine

  isFound := FindFirst(sPath+'*.*', fsAnyFile-fsAnyDirectory, SR) = 0;

  while isFound do

  begin

    if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') then

        ShowMessage('Filename: '+SR.Name); // Do whatever you want with the filename

    IsFound := FindNext(SR) = 0;

  end;

  FindClose(SR);

end;

...

This example will only find files within the directory provided by sPath, but of course you  can alter that to your needs. If you want to recursively walk down a directory path and get both subdirectories and files, then you'll have to change the code that ignores directories and instead check the attr. If the record is a file, process the flie as you want. If it's a directory then do what you need to with it (e.g. store the name, etc) then pass that in to the same routine recursively as a path to be searched. IMPORTANT: Just be sure that each pass through the routine has a way out, that it will end at some point. And that each time it ends, it calls the FindClose() construct on the version of the search rec allocated for that pass.

Hope that helps,

Larry

 Posted 9 years ago
Comment Quote
About Larry Rutledge -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
About Larry Rutledge

I worked for Prestwood Software as a Delphi developer from 1997 through 2002. During that time I enjoyed working with Mike Prestwood and the other developers at Prestwood.


Post ID #14153 (Level 1.2)  Reply to 14146
Reply Posted 6/7/2010 4:05:09 PM
Location=-- USA 
Joined=11 years ago   MB Posts=11   KB Comments=1  
Most Recent Post
Wes Peterson
Prestwood IT
Prestwood IT office in Citrus Heights, CA

Hi Larry,

Thanks for the great post putting some meat on the bones of my suggestions. There's nothing like good sample code to get one started. I wanted to post some, but time didn't permit. Glad you did.

Wes Peterson
Senior Software Engineer
Prestwood IT Solutions
http://www.prestwood.com

 Posted 9 years ago
Comment Quote
About Wes Peterson -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
About Wes Peterson

Wes Peterson is a Senior Programmer Analyst with Prestwood IT Solutions where he develops custom Windows software and custom websites using .Net and Delphi. When Wes is not coding for clients, he participates in this online community. Prior to his 10-year love-affair with Delphi, he worked with several other tools and databases. Currently he specializes in VS.Net using C# and VB.Net. To Wes, the .NET revolution is as exciting as the birth of Delphi.


Post ID #14163 (Level 2.1)  Reply to 14153 and Parent is 14146
Thread Started 6/10/2010 8:00:33 AM
Location=Prestwood IT office in Citrus Heights, CA 
Joined=15 years ago   MB Posts=158   KB Posts=163   KB Comments=34   BLOG, Topics=20  

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1186A1
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #14146 Counter
3974
Since 6/7/2010

Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech