Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-03

function to do common things to files a directory (possibly recursive)

7 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

leledumbo

I often needed to delete files recursively (of course only those matched given mask), so I create a program to help me doing it (Windows explorer's search is SLOW and the deletion is even SLOWER!). However, the running time is O(2n). Since I need to delete tens of thousands of files, the running time is quite significant.

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?

Possible interface:
type
  TFileAction = procedure (const FileName: String);
procedure IterateDir(const BasePath,FileMask: String; const Recursive:
Boolean: Action: TFileAction);

P.S.: Attachment is the source code of my program. http://www.nabble.com/file/p22655474/Deleter.pas Deleter.pas

View this message in context: http://www.nabble.com/function-to-do-common-things-to-files-a-directory-%28possibly-recursive%29-tp22655474p22655474.html Sent from the Free Pascal - General mailing list .
Bart
to leledumbo
leledumbo wrote 6 lines
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

leledumbo
to Bart

Nice work Bart, I also see that you've a lot of useful units there. I already use 2.3.1 so that'll be OK. Let the download begins...

OOT: My project http://code.google.com/p/express-gui ExPress uses a unit called browserdetection. But I don't see any author name in the comment, please check if it yours. I'll add your name in the next version.

View this message in context: http://www.nabble.com/function-to-do-common-things-to-files-a-directory-%28possibly-recursive%29-tp22655474p22673687.html Sent from the Free Pascal - General mailing list .
Bart
to leledumbo
OOT: My project http://code.google.com/p/express-gui ExPress uses a unit called browserdetection. But I don't see any author name in the comment, please check if it yours. I'll add your name in the next version.

I can not find any sourcecode on
http: can check my BrowserDetect unit, to see if it is the same. OTOH I based my code on code snippets from several others, so no need to mention my name specifically ;-)

Bart

leledumbo
to Bart
I can not find any sourcecode on http: don't have svn access, so I put it in the download section. You can check my BrowserDetect unit, to see if it is the same.

I'll take a look.

OTOH I based my code on code snippets from several others, so no need to mention my name specifically ;-)

Still, you're the author of the unit :-)

View this message in context: http://www.nabble.com/function-to-do-common-things-to-files-a-directory-%28possibly-recursive%29-tp22655474p22676862.html Sent from the Free Pascal - General mailing list .