[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.

http://home.tiscali.nl/~knmg0017/software/fpc_laz/enumdirs.zip
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
(http://home.tiscali.nl/~knmg0017/laz_fpc_libs.htm)

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

In essence:

var Enumerator: TDirEnumerator;

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

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
FindFirstUTF8/FindNextUTF8?
I did not test this, as I never needed it, and I wrote the unit befoer
the switch in LCL to UTF8)

Bart



More information about the fpc-pascal mailing list