Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to find files with wildcards Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
20-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
73
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How can I find files using wildcards? For example:

1   wildcards('c:\*.txt', 'c:\test.txt') = true
2   wildcards('*.c?g', '123.cfg') = true
3   wildcards('c*.doc', 'doc.doc') = false


Answer:

4   type
5     PathStr = string[128]; { in Delphi 2/3: = string }
6     NameStr = string[12]; { in Delphi 2/3: = string }
7     ExtStr = string[3]; { in Delphi 2/3: = string }
8   
9   {$V-}
10    { in Delphi 2/ 3 to switch off "strict var-strings" }
11  
12  function WildComp(FileWild, FileIs: PathStr): boolean;
13  var
14    NameW, NameI: NameStr;
15    ExtW, ExtI: ExtStr;
16    c: Byte;
17  
18    function WComp(var WildS, IstS: NameStr): boolean;
19    var
20      i, j, l, p: Byte;
21    begin
22      i := 1;
23      j := 1;
24      while (i <= length(WildS)) do
25      begin
26        if WildS[i] = '*' then
27        begin
28          if i = length(WildS) then
29          begin
30            WComp := true;
31            exit
32          end
33          else
34          begin
35            { we need to synchronize }
36            l := i + 1;
37            while (l < length(WildS)) and (WildS[l + 1] <> '*') do
38              inc(l);
39            p := pos(copy(WildS, i + 1, l - i), IstS);
40            if p > 0 then
41            begin
42              j := p - 1;
43            end
44            else
45            begin
46              WComp := false;
47              exit;
48            end;
49          end;
50        end
51        else if (WildS[i] <> '?') and ((length(IstS) < i) or (WildS[i] <> IstS[j])) 
52  then
53        begin
54          WComp := false;
55          exit
56        end;
57        inc(i);
58        inc(j);
59      end;
60      WComp := (j > length(IstS));
61    end;
62  
63  begin
64    c := pos('.', FileWild);
65    if c = 0 then
66    begin { automatically append .* }
67      NameW := FileWild;
68      ExtW := '*';
69    end
70    else
71    begin
72      NameW := copy(FileWild, 1, c - 1);
73      ExtW := copy(FileWild, c + 1, 255);
74    end;
75    c := pos('.', FileIs);
76    if c = 0 then
77      c := length(FileIs) + 1;
78    NameI := copy(FileIs, 1, c - 1);
79    ExtI := copy(FileIs, c + 1, 255);
80    WildComp := WComp(NameW, NameI) and WComp(ExtW, ExtI);
81  end;
82  
83  { Example }
84  begin
85    if WildComp('a*.bmp', 'auto.bmp') then
86      ShowMessage('OK 1');
87    if not WildComp('a*x.bmp', 'auto.bmp') then
88      ShowMessage('OK 2');
89    if WildComp('a*o.bmp', 'auto.bmp') then
90      ShowMessage('OK 3');
91    if not WildComp('a*tu.bmp', 'auto.bmp') then
92      ShowMessage('OK 4');
93  end;
94  
95  end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC