FindNext returns file names even when used only with faDirectory

I am trying to list all the directories in this directory. I have this code:

var srec: TSearchRec; begin // folder is some absolute path of a folder if FindFirst(folder + PathDelim + '*', faDirectory, srec) = 0 then try repeat if (srec.Name <> '.') and (srec.Name <> '..') then ShowMessage(srec.Name); until FindNext(srec) <> 0; finally FindClose(srec); end; 

But for some reason, I get messages about file names, not just directories. I thought using faDirectory would make FindFirst and family would return directory names. What am I doing wrong? If I change it to

 if FindFirst(folder, faDirectory, srec) = 0 then 

Then it displays only the name folder , but not as an absolute path (relative to folder + '/..' ) and after that it terminates.

I understand that I can check if this is a directory, making sure (srec.Attr and faDirectory) = faDirectory , but I feel like I am doing things in a workaround, and there should be a proper way to do this.

+4
source share
3 answers

If you are using delphi xe, check out the function TDirectory.GetDirectories .

SysUtils.FindFirst The documentation has an answer to your problem.

 function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer; 

The Attr parameter specifies special files to include in addition to all normal files . Choose from these file attribute constants when specifying the Attr parameter.

+6
source

You can do something like this:

 var Dir: string; begin for Dir in TDirectory.GetDirectories('c:\') do ShowMessage(Dir); end; 
+4
source

You should use a filter to delete files; some small changes in your code

your code: folder + PathDelim + '*' change to

 folder + PathDelim + '*.' 
+1
source

Source: https://habr.com/ru/post/1388561/


All Articles