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!

set start point for SelectDirectory? 1

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
Hi,

I want to call the SelectDirectory function, for the user to browse to a desired folder, but I want to set the initial startpoint.
I have this code so far:


procedure TSDIAppForm.Button1Click(Sender: TObject);

var
Dir: String;

begin
SetCurrentDir('F:\PCB\Altium\Projects');
SelectDirectory('Select a directory', '', Dir);
Folderlabel.Caption := (Dir);
end;


The idea is that when I click the button, the SetCurrentDir would start the SelectDirectory off in F:\PCb etc. But this isn't working, the SelectDirectory always starts in desktop as far as I can tell.

What is it I am missing?

Tenchy
Delphi 11 on XP PRo.
 
Try something like
Code:
procedure TSDIAppForm.Button1Click(Sender: TObject);
var
  Dir: String;
begin
  SelectDirectory('Select a directory', 'F:\PCB\Altium\Projects', Dir);
  Folderlabel.Caption := Dir;
end;

Andrew
Hampshire, UK
 
Hi Andrew,
Excellent thanks a lot.

Now I can see the the already in place empty quotes ready to accept my path. Doh!

Steve
 
Hmm. Now I have that sorted, I have a new failure in this section.
I'm trying to list the contents of a directory in a listbox called sourcebox.

What I find at runtime is that using SelectDirectory doesn't seem to have any success with my code. Selecting any directory results in nothing being dispayed.
If I Cancel the SelectDirecory dialog, I get listed in Sourcebox the contents of my delphi project folder on C:
When it does list the contents of my project folder, my FolderLabel.Caption remains blank (which is fair enough I guess if Cancel'ing the dialog)

Here is the code, am I missing another simple thing?

Steve


procedure ListFileDir(Path: String; FileList: TStrings);

var
SR: TSearchRec;

begin
if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
begin
repeat
if (SR.Attr <> faDirectory) then
begin
FileList.Add(SR.Name);
end;
until FindNext(SR) <> 0;
FindClose(SR);
end;
end;


// Get the folder to work on
procedure TSDIAppForm.Button1Click(Sender: TObject);

var
Dir: String;

begin
SelectDirectory('Select a directory', 'F:\PCB\Altium\Projects\', Dir);
Folderlabel.Caption := (Dir);
ListFileDir(Dir, Sourcebox.Items);
end;
 
A couple of things.

It is usually a good idea to ensure that the path ends with a '\'. This is best done using the function IncludeTrailingPathDelimiter

The reason why your SourceBox is empty is because the first parameter you have given to FindFirst is something like F:\PCB\Altium\Projects*.* and I guess there aren't any files in the Altium folder that have a name beginning with Projects

Also the attribute parameter used in FindFirst is really a set of flags. So you should test individual bits in the flag and not use <>.

Andrew
Hampshire, UK
 
In the words of Mr Burns, "Ah exxxxcellent...."

Thanks again.

Steve
 
one more Q please:
Should this line:
if (SR.Attr <> faDirectory) then

from this section:
repeat
if (SR.Attr <> faDirectory) then
begin
FileList.Add(SR.Name);
end;
until FindNext(SR) <> 0;
FindClose(SR);


not exclude directories from being added to the list items?

I ask as directories are definately being added, but I only want files added.

Steve
 
Like Towerbase said, SR.Attr is a set of flags.
so you need to test it

all files except directories:
(SR.Attr And faDirectory) <> faDirectory

all files with archive bit set
(SR.Attr And faArchive) = faArchive

and so on...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
also weed out
(SR.name <> '.') and (SR.name <> '..') from your file list as those 2 are also directories

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks whosrdaddy, the first test sorted it it, including the dos type directory two.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top