The MatchPattern function returns the boolean value True if the string Source matches with the pattern specified by the parameter Pattern.

This matching function is intended for matching file paths and file names, and uses the wildcard characters defined for Windows file searches, i.e. * for zero or more matching characters, and ? for a match in the specified character position.

The values for Source and Pattern should not exceed the maximum length for file names (255 characters).

For generic string matching and manipulation functions, not involving file names, please refer to String Functions.

Declaration: Function MatchPattern( source, pattern : String) : Boolean

A quick example would be as follows.

procedure OnMapEvent(var Value:Variant);
begin
if MatchPattern('test.txt', '*.txt') then //returns true
LogInfo('The file is of type .txt');
 
if MatchPattern('c:\program files\test.txt', '*.txt') then //also returns true
LogInfo('The file is of type .txt');
 
end;

The following tests for matches will return True:

  • MatchPattern('TEST.TXT', '*.txt')

  • MatchPattern('test.txt', '*.TXT')

  • MatchPattern('test.txt', '????.txt')

  • MatchPattern('c:\program files\test.txt', 'c:\*\test.txt')

  • MatchPattern('c:\program files\test.txt', 'c:\*\*.txt')

  • MatchPattern('c:\program files\test.txt', 'c:\*.txt')

The following tests for matches will return False:

  • MatchPattern('test.txt', '?.txt') // too many characters

  • MatchPattern('t.txt', '????.txt') // not enough characters

These are illustrated by the code segment as below.

procedure ScriptEvent (var Value : variant);
var
//Initialise the variabled for the "source" and "pattern" arguments
s, p: string;
begin
s := 'test.txt'; p := '*.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
s := 'c:\program files\test.txt'; p := '*.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
 
 
s := 'TEST.TXT'; p := '*.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
s := 'test.txt'; p := '*.TXT'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
s := 'test.txt'; p := '????.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
s := 'test.txt'; p := '?.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
s := 't.txt'; p := '????.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
s := 'c:\program files\test.txt'; p := 'c:\*\test.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
s := 'c:\program files\test.txt'; p := 'c:\*\*.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
s := 'c:\program files\test.txt'; p := 'c:\*.txt'; LogInfo('MatchPattern(''' + s + ''', ''' + p + ''') is ' + BoolToFloString(MatchPattern(s, p)));
 
end;

The resulting entries in the log is as follows.

image-20240108-221216.png