{"id":115,"date":"2007-05-12T14:27:37","date_gmt":"2007-05-12T12:27:37","guid":{"rendered":" http:\/\/www.nsonic.de\/blog\/2007\/05\/check-if-file-matches-wildcards\/"},"modified":"2007-05-12T14:27:39","modified_gmt":"2007-05-12T12:27:39","slug":"check-if-file-matches-wildcards","status":"publish","type":"post","link":"https:\/\/www.nsonic.de\/blog\/2007\/05\/check-if-file-matches-wildcards\/","title":{"rendered":"Check if File matches Wildcards"},"content":{"rendered":"<p><em>Q:<br \/>\n> i would like to make a function that return if the file is in that<br \/>\nwildcards<br \/>\n><br \/>\n> e.g.<br \/>\n> wildcards(&#8216;c:\\*.txt&#8217;,&#8217;c:\\test.txt&#8217;)   = true<br \/>\n> wildcards(&#8216;*.c?g&#8217;,&#8217;123.cfg&#8217;) = true<br \/>\n> wildcards(&#8216;c*.doc&#8217;,&#8217;doc.doc&#8217;) = false<br \/>\n><br \/>\n> thanks<br \/>\n<\/em><br \/>\n<!--more--><br \/>\nA:<br \/>\n[code lang=&#8221;delphi&#8221;]type<br \/>\n  PathStr = string[128]; { in Delphi 2\/3: = string }<br \/>\n  NameStr = string[12];  { in Delphi 2\/3: = string }<br \/>\n  ExtStr  = string[3];   { in Delphi 2\/3: = string }<\/p>\n<p>{$V-} { in Delphi 2\/ 3 to switch off &#8220;strict var-strings&#8221; }<\/p>\n<p>function WildComp(FileWild,FileIs: PathStr): boolean;<br \/>\nvar<br \/>\n  NameW,NameI: NameStr;<br \/>\n  ExtW,ExtI: ExtStr;<br \/>\n  c: byte;<\/p>\n<p>  function WComp(var WildS,IstS: NameStr): boolean;<br \/>\n  var<br \/>\n    i, j, l, p : Byte;<br \/>\n  begin<br \/>\n    i := 1;<br \/>\n    j := 1;<br \/>\n    while (i<=length(WildS)) do\n    begin\n      if WildS[i]='*' then\n      begin\n        if i = length(WildS) then\n        begin\n          WComp := true;\n          exit\n        end\n        else\n        begin\n          { we need to synchronize }\n          l := i+1;\n          while (l < length(WildS)) and (WildS[l+1] <> &#8216;*&#8217;) do<br \/>\n            inc (l);<br \/>\n          p := pos (copy (WildS, i+1, l-i), IstS);<br \/>\n          if p > 0 then<br \/>\n          begin<br \/>\n            j := p-1;<br \/>\n          end<br \/>\n          else<br \/>\n          begin<br \/>\n            WComp := false;<br \/>\n            exit;<br \/>\n          end;<br \/>\n        end;<br \/>\n      end<br \/>\n      else<br \/>\n      if (WildS[i]<>&#8216;?&#8217;) and ((length(IstS) < i)\n      or (WildS[i]<>IstS[j])) then<br \/>\n      begin<br \/>\n        WComp := false;<br \/>\n  exit<br \/>\n      end;<\/p>\n<p>      inc (i);<br \/>\n      inc (j);<br \/>\n    end;<br \/>\n    WComp := (j > length(IstS));<br \/>\n  end;<\/p>\n<p>begin<br \/>\n  c:=pos(&#8216;.&#8217;,FileWild);<br \/>\n  if c=0 then<br \/>\n  begin { automatically append .* }<br \/>\n    NameW := FileWild;<br \/>\n    ExtW  := &#8216;*&#8217;;<br \/>\n  end<br \/>\n  else<br \/>\n  begin<br \/>\n    NameW := copy(FileWild,1,c-1);<br \/>\n    ExtW  := copy(FileWild,c+1,255);<br \/>\n  end;<\/p>\n<p>  c:=pos(&#8216;.&#8217;,FileIs);<br \/>\n  if c=0 then<br \/>\n    c:=length(FileIs)+1;<br \/>\n  NameI := copy(FileIs,1,c-1);<br \/>\n  ExtI  := copy(FileIs,c+1,255);<br \/>\n  WildComp := WComp(NameW,NameI) and WComp(ExtW,ExtI);<br \/>\nend;<\/p>\n<p>begin<br \/>\n  if     WildComp(&#8216;a*.bmp&#8217;,  &#8216;auto.bmp&#8217;) then ShowMessage(&#8216;OK 1&#8217;);<br \/>\n  if not WildComp(&#8216;a*x.bmp&#8217;, &#8216;auto.bmp&#8217;) then ShowMessage(&#8216;OK 2&#8217;);<br \/>\n  if     WildComp(&#8216;a*o.bmp&#8217;, &#8216;auto.bmp&#8217;) then ShowMessage(&#8216;OK 3&#8217;);<br \/>\n  if not WildComp(&#8216;a*tu.bmp&#8217;,&#8217;auto.bmp&#8217;) then ShowMessage(&#8216;OK 4&#8217;);<br \/>\nend.<br \/>\n[\/code]<\/p>\n<p>[tags]Delphi, File[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: > i would like to make a function that return if the file is in that wildcards > > e.g. > wildcards(&#8216;c:\\*.txt&#8217;,&#8217;c:\\test.txt&#8217;)&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[11],"tags":[75,99],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p71Tml-1R","_links":{"self":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/115"}],"collection":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":0,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}