Recent

Author Topic: Procedure that undestand wildcard  (Read 626 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 388
Procedure that undestand wildcard
« on: November 05, 2025, 03:59:14 pm »
Hello.

I need to delete files from directory that is pointed like
Quote
*.txt
*.pas
And so on
Is there a procedure that undestand a wildcard?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

d2010

  • Full Member
  • ***
  • Posts: 235
Re: Procedure that undestand wildcard
« Reply #1 on: November 05, 2025, 04:28:47 pm »
Old-Style , but you do not delete inside deep-all-subdirs.
Code: [Select]
Function SHFileOperation_FO_DELTREE(filepath: h_types.TFileName3):string;
    var
      tfileinfo: TSearchRec;
      r: TFileName;
      retcode,atribute: longint;
    begin
      result:='';
      r := filepath + '\*';
      writeln(r);
      retcode := SysUtils.FindFirst(r, faAnyFile - faVolumeID, tfileinfo);
      while retcode = 0 do
        begin
          if (tfileinfo.attr and faDirectory = faDirectory) then
            begin
            if (tfileinfo.name <> '.') and (tfileinfo.name <> '..') then
                result:=concat(result,SHFileOperation_FO_DELTREE(filepath + '\' + tfileinfo.name));
            end
          else
            begin
              atribute := tfileinfo.attr;
              atribute := atribute and not faReadOnly;
              atribute := atribute and not faArchive;
              atribute := atribute and not faSysFile;
              atribute := atribute and not faHidden;
              FileSetAttr(filepath + '\' + tfileinfo.name, atribute);
              if DeleteFile(pchar(filepath + '\' + tfileinfo.name)) = false then
                 result:=concat(result,filepath + '\' + tfileinfo.name,#13#10);
            end;
          retcode := SysUtils.FindNext(tfileinfo);
        end;
      SysUtils.FindClose(tfileinfo);
      atribute := FileGetAttr(filepath);
      atribute := atribute and not faReadOnly;
      atribute := atribute and not faArchive;
      atribute := atribute and not faSysFile;
      atribute := atribute and not faHidden;
      FileSetAttr(filepath, atribute);
      if RemoveDir(filepath) = false then
            result:=concat(result,'RMdir ', filepath,' =Failed'#13#10);
    end;


Code: [Select]
Procedure DelFilesFromDir(Const Directory, FileMask: string; DelSubDirs: boolean);
      var
        SourceLst: string;
        FOS: TSHFileOpStruct;
      begin
        FillChar(FOS, sizeof(FOS), 0);
        // FOS.Wnd := Application.MainForm.Handle;
        FOS.Wnd := application_handle;
        FOS.wFunc := FO_DELETE;
        SourceLst := Directory + '\' + FileMask + #0;
        FOS.pFrom := pchar(SourceLst);
        if not DelSubDirs then
          FOS.fFlags := FOS.fFlags OR FOF_FILESONLY;
        // Remove the next line if you want a confirmation dialog box
        FOS.fFlags := FOS.fFlags OR FOF_NOCONFIRMATION;
        // Uncomment the next line for a "silent operation" (no progress box)
        // FOS.fFlags := FOS.fFlags OR FOF_SILENT;
        SHFileOperation(FOS);
      end;
« Last Edit: November 05, 2025, 04:37:32 pm by d2010 »

Thaddy

  • Hero Member
  • *****
  • Posts: 18475
  • Here stood a man who saw the Elbe and jumped it.
Re: Procedure that undestand wildcard
« Reply #2 on: November 05, 2025, 04:43:59 pm »
Look into the sources of the utility delp that comes with fpc and has a cross platform way of deleting things safely.
Just copy and paste.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

paweld

  • Hero Member
  • *****
  • Posts: 1514
Re: Procedure that undestand wildcard
« Reply #3 on: November 05, 2025, 04:47:25 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.   FileUtil;
  3.  
  4. var
  5.   sl: TStringList;
  6.   i: Integrr;
  7. begin
  8.   sl := FindAllFiles('d:\my files' {path}, '*.txt;*.pas' {mask}, False {search in subdirectories});
  9.   for i := 0 to sl.Count - 1 do
  10.     DeleteFile(sl[i]);
  11.   sl.Free;
  12. end
« Last Edit: November 05, 2025, 04:49:01 pm by paweld »
Best regards / Pozdrawiam
paweld

LemonParty

  • Sr. Member
  • ****
  • Posts: 388
Re: Procedure that undestand wildcard
« Reply #4 on: November 05, 2025, 04:58:49 pm »
Thank you paweld. That is what I looking for.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thaddy

  • Hero Member
  • *****
  • Posts: 18475
  • Here stood a man who saw the Elbe and jumped it.
Re: Procedure that undestand wildcard
« Reply #5 on: November 05, 2025, 05:02:38 pm »
Be carefull to use it for deletions, though... I warned you.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Bart

  • Hero Member
  • *****
  • Posts: 5640
    • Bart en Mariska's Webstek
Re: Procedure that undestand wildcard
« Reply #6 on: November 05, 2025, 06:41:17 pm »
The Masks unit has functions for that purpose exactly (FindAllFiles() uses them).

Bart

n7800

  • Hero Member
  • *****
  • Posts: 583
  • Lazarus IDE contributor
    • GitLab profile
Re: Procedure that undestand wildcard
« Reply #7 on: November 05, 2025, 10:15:54 pm »
Look into the sources of the utility delp that comes with fpc and has a cross platform way of deleting things safely.
Just copy and paste.

What exactly do you mean by "safely" delete? A quick glance at the source didn't reveal anything.

 

TinyPortal © 2005-2018