[fpc-pascal] function to do common things to files a directory (possibly recursive)

Bart bartjunk64 at gmail.com
Mon Mar 23 12:18:20 CET 2009

On 3/23/09, leledumbo <leledumbo_cool at yahoo.co.id> wrote:
>  This can be achieved by a function that iterates over a directory (and
>  possibly its subdirectories) and do specified operation on every files that
>  matches given mask. However, I don't seem to find one. The closest one is
>  FindAllFiles from Lazarus' FileUtil unit + iteration of DeleteFile over the
>  result and that's the one I'm currently using. Would someone add it?
See my EnumDirs unit.
It does simply that. You can set include and exclude masks.
Masks can be case-sensitive or not, unixtype or windowstype, just set
the appropriate option
(I use this unit as a base for my backup program (see sources at
http://home.tiscali.nl/~knmg0017/software/sbp_bron.zip for example).

The unit is documented in the sourcecode.

You also need extmasks.zip (and possibly fpc_patches.tgz if you don't
use fpc 2.3.x), they ar all on the same page

  TDirEnumerator = class(TCustomDirEnumerator)
    property Path;
    property MaskList;
    property ExclMaskList;
    property ListSeparator;
    property Attribute;
    property Options;
    property FileHandler;

In essence:

var Enumerator: TDirEnumerator;

    Enumerator := TDirEnumerator.Create;
    Enumerator.Path := '/';
    Enumerator.MaskList := '*';
    Enumerator.Options := [soRecursive, soIgnoreErrors];
    Enumerator.Attribute := faAnyFile or faSymLink;
    Enumerator.FileHandler := @DeleteTheFile;

This would nicely delete all files (in the DeleteTheFile method) on your system.
Run it as root ;-) to have fun

(Possibly the cals to FindFirst/FindNext should be replaced by
I did not test this, as I never needed it, and I wrote the unit befoer
the switch in LCL to UTF8)


More information about the fpc-pascal mailing list