Jim Jim - 5 months ago 37
PHP Question

Pascal isDir function

I want to make a program that lists files contained in a certain directory. I want to list the file and only the file, not the folders that is on this directory, too. How can I do this in Pascal? If in PHP, there is a function named "isDir", which return true if the parameter specified is a directory(folder). Using this function, I could check whether the "path" I'm using is a file or a folder.

Now, I am using the FindFirst and FindNext procedure to get all files/folders in the directory.

program mine;
uses dos;

var F: SearchRec
begin
readln(dir);
FindFirst('dir+'\.*.',anyFile,F);
while DosError=0 do begin
writeln(F.Name);
FindNext(F);
end;
end.

Answer Source

Try the following

while DosError=0 do begin
        if ((F.attr and faDirectory) <> faDirectory)
         then  writeln(F.Name);
        FindNext(F);
    end;

Sorry my Pascal is a bit rusty

Basically the object returned by FindFirst/FindNext contains an ATTR property which indicated the type of file it is.

Also, at the end of your loop, be sure to add a FindClose() call to free the memory

FindClose(F)

File attribute constants

  • faDirectory-Is directory
  • faReadOnly-Readonly file
  • faArchive-Archive bit
  • faHidden-File is hidden
  • faVolumeId-File is the volumne ID
  • faSysFile-System File