tags:

views:

274

answers:

4

the following code lists files but not directories

var 
rec : tsearchrec;
begin
findfirst('c:\test\*',faanyfile-fadirectory,rec);
        showmessage(rec.Name);
if findnext(rec) <> 0 then close else
showmessage (rec.Name);
end;
+2  A: 

What about findfirst('c:\test\*', faanyfile, rec); // not faanyfile-fadirectory

utku_karatas
A: 

If you want all files and directories, just pass in faDirectory to findfirst. It's going to already return you files.

Joe
And hope that it will never get fixed and return only directories as expected...
Smasher
I'm just going based on the voluminous documentation that's out there.
Joe
But why would you pass faDirectory when you can just pass faAnyFile, even if you can rely on the fact that this won't change? That's just not readable.
Smasher
+2  A: 

You explicitly excluded directories by stating " - faDirectory" in the flags parameter.

devio
+13  A: 
  1. You need to NOT exclude the directories, which you unfortunately do with your (-faDirectory)
  2. You have to call FindClose when you're done.
  3. You need to loop if you want to find everything in the directory
var
 rec : tsearchrec;
begin
  if FindFirst('c:\*', faAnyFile, rec) = 0 then
  begin
    repeat
      ShowMessage(rec.Name);
    until FindNext(rec) <> 0;
    FindClose(rec);
  end;
end;
François
thanks, it is showing directories now but one more problem it is returning '..' and '.' in showmessage as well as files and folders, what are they i switched ''show hidden files option on'' but still cant see any '.' and '..' please help!
Omair Iqbal
also i tried to show 'just directories' using '-faanyfile or fadirectory' in the attributes constant so than it does not show any files but just directory but it is still returning files
Omair Iqbal
Omair, "." and ".." are directory entries, just like any other directory entry. You simply have to make sure your code can handle them. Also, you've probably realized that `FirstFirst` isn't very good at *excluding* results. Common practice is to use `FindFirst` and `FindNext` to enumerate *all* files and directories, and then check for the criteria you need in your own code and skip the ones you don't want.
Rob Kennedy
but what are '.' and '..' for? are they directories and if they are why cant i access them even from command prompt? 2.is there a good alternative to findfirst?.3. while i dont know c++ does c++ have a good findfirst like function that is better at excluding results?
Omair Iqbal
@Omair: "." is an alias for the current directory and ".." is the parent directory. If you type "cd .." at the command prompt, you will move from the current directory to its parent.
Bruce McGee
@Omair. Please read the help entry for FindFirst. If you want only the directories, just use the flag faDirectory. Go and look at where it is defined and you'll see all available option that you can combine with or. (faAnyfile being already a combination in itself). Now as Rob explained, build the flag to encompass all the files you need and select those you really want inside the loop with an if condition meeting your specific criteria.
François
yes but why does findfirst list them as directories they are just DOS era functions...is something wrong with windows or delphi or am i making an issue out of an unimportant matter :)
Omair Iqbal
Yes, you're making a mountain out of a molehill. Directories are stored as files on disk, and their contents are all the things you can get to from that directory. You can go from the directory to its parent, so each directory has a ".." entry. Likewise for the "." entry; you can choose to go nowhere. That's how file systems work. File systems are DOS-era things, since they existed during the DOS era. But we still use them today. I use them at the command line every day to refer to things relative to the current directory, and yet I haven't used DOS since the '90s.
Rob Kennedy