Jump to content

WildcardMatch an Fast Filename Pattern Match (UDF) - Machine Code Version

Recommended Posts

WildcardMatch an Fast Filename Pattern Match (UDF)

Machine Code Version (x32 and x64)

Repeat the word from an C/C++ Writer {


Simple wild card matching with ? and * is something that we use in our every day work
just when opening a command prompt and using DIR and DEL.
But how can this be done correctly in your program?

OK, you can use regular expressions, and I recommend this when you already use a
bunch of Boost, tr1 or STL stuff in your code. But sometimes, I like it easy and simple
without tons of library code in the background. And because I saw a lot of wrong and
in complex queries "wrong/failing" code, I just offer this small algorithm here.


This wildcard matching function works just the same way as you expect and know it from
the CMD.EXE DIR command.

Using the Code

The function just takes the string to check as a first argument and the mask with or without
any wildcard characters as a second argument. It returns true if the strings match and false
if not. It isn't spectacular.

The characters ? and * are treated as wildcards.
A ? character matches exactly one character and doesn't match an empty string.
A * character matches any sequence of characters and an empty string too.
Other characters are compared caseless. I use CharUpper and convert them to uppercase

to perform this task. Feel free to use your own favorite way.

Because * matches any character sequence and an empty string WildcardMatch(_T(""),_T("*"))
returns true.

} by Martin Richter, 28 Apr 2011 on codeproject.com

AutoIt3 WildcardMatch

No, I'm not doing any translation the C/C++ WildcardMatch to our favorite scripting
language: AutoIt V3. Instead modificate thats source to be:

- Ansi and Wide text version of WildcardMatch.
- Portable and meet the condition to be used on

  Shell Code or Machine Code (That what Ward, trancexx and other called it!).

And then write an UDF.

UDF Name  : CoreFx.Wildcard.au3
Functions : CoreFx_WildcardMatchExA($string, $patern)
            CoreFx_WildcardMatchExW($string, $patern)
Includes  : CoreFx.DynamicCode.au3
Platform  : x32 and x64 bit
Sample    : $test.au3

Download In Single ZIP (File Size: 5 KB. All required file are included).



$test.au3 Outputs:

Ansi Test

Empty string 1 CoreFx_WildcardMatchExA("", "*") Return 1
Empty string 2 CoreFx_WildcardMatchExA("", "") Return 1
Simple 1 CoreFx_WildcardMatchExA("MyName.doc", "*.DOC") Return 1
Simple 2 CoreFx_WildcardMatchExA("MyName.docx", "*.DOC") Return 0
Complex 1 CoreFx_WildcardMatchExA("MyNamName.docx", "My*Name.*x") Return 1
Complex 2 CoreFx_WildcardMatchExA("MyNamName.docx", "My*Name.*oc") Return 0
A CoreFx_WildcardMatchExA("filename.txt", "*.txt") Return 1
B CoreFx_WildcardMatchExA("filename.txt", "*.tx?") Return 1
C CoreFx_WildcardMatchExA("filename.txt", "file*name.txt") Return 1
D CoreFx_WildcardMatchExA("filename.txt", "fil*ame.txt") Return 1
E CoreFx_WildcardMatchExA("filename.txt", "f*l*a*e.*x?") Return 1

Wide Test

Empty string 1 CoreFx_WildcardMatchExW("", "*") Return 1
Empty string 2 CoreFx_WildcardMatchExW("", "") Return 1
Simple 1 CoreFx_WildcardMatchExW("MyName.doc", "*.DOC") Return 1
Simple 2 CoreFx_WildcardMatchExW("MyName.docx", "*.DOC") Return 0
Complex 1 CoreFx_WildcardMatchExW("MyNamName.docx", "My*Name.*x") Return 1
Complex 2 CoreFx_WildcardMatchExW("MyNamName.docx", "My*Name.*oc") Return 0
A CoreFx_WildcardMatchExW("filename.txt", "*.txt") Return 1
B CoreFx_WildcardMatchExW("filename.txt", "*.tx?") Return 1
C CoreFx_WildcardMatchExW("filename.txt", "file*name.txt") Return 1
D CoreFx_WildcardMatchExW("filename.txt", "fil*ame.txt") Return 1
E CoreFx_WildcardMatchExW("filename.txt", "f*l*a*e.*x?") Return 1



A Simple Wildcard Matching Function (Martin Richter [MVP C++])


Wildcard Character

For modified version of WildcardMatch (C++ source). If you need it, just pm me!

# Button. Progressbar - Graphical AutoIt3 Control (UDF) # GTK on AutoIt3 - GTK+ Framework | Widgets

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Blitzkid
      Hello, i want to search several directories for files with the largest numbers behind them (Like "video123") . They dont have a datatype. But there are also files with longer names and datatypes in these folders (Like "video778.mp4"). Is it possible to filter the _FileListToArray Syntax from
      to smth. like
      Here is my Code
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> #include <File.au3> $filedir = @ScriptDir & "\" _checkfile() Func _checkfile() ConsoleWrite("______________________" & @CRLF) Local $arr[3] = ["music", "picture", "video"] For $i = 0 To UBound($arr) - 1 Local $arrayfiles = _FileListToArray($filedir & $arr[$i], $arr[$i] & "*", 1) If @error = 1 Then ConsoleWrite($arr[$i] & "Error 1") EndIf If @error = 4 Then ConsoleWrite($arr[$i] & "Error 2") ;Exit EndIf $arrayfilter = _ArrayMax($arrayfiles, 0, 1) Global $stringfiles = StringReplace($arrayfilter, $arr[$i], "") ConsoleWrite($arrayfilter & @CRLF) Next EndFunc ;==>_checkfile  
    • By drorshem
      I'm looking to create a script which will download all *.txt files from a remote FTP server.
      Once downloaded, delete all *.txt files.
      Can this be achieved using AutoIt ?
    • By RaiNote
      How to create Wildcards to search for a Filename and also for a String within a File and within the own script.
    • By jguinch
      In a folder with some files that have no extension (for example the hosts file in C:\Windows\System32\driversetc), FileListToArrayRec does not return the same thing than _FileListToArray if I use *.* as filter :
         _FileListToArray("C:\Windows\System32\driversetc", "*") : returns all files
         _FileListToArray("C:\Windows\System32\driversetc", "*.*") : returns all files
         _FileListToArrayRec("C:\Windows\System32\driversetc", "*") : returns all files
         _FileListToArrayRec("C:\Windows\System32\driversetc", "*.*") : returns only files with extensions
      It seems logic that FileListToArrayRec and FileListToArray should have the same behaviour with the same filter, no ?
      It's not important at all for me, but maybe developers will want to ensure that these two functions react in the same way...
      Edit : sorry if this post is not in the good section. Please move it if needed.
    • By Kidney
      So im making a script that organizes TV shows. i have figured most of it out but im trying to find the portion of the title that contains the Season number and Episode number. (ie S01E13)
      is there and easy way to search for the "S" and "E" in a string with any 2 random numbers in between them and after the "E"?
      here is what i tried just as a very wild guess:
      Func FindSeasonEpisode($FileName) $result = StringInStr($FileName, "S**E**") ConsoleWrite($result & @CRLF) EndFunc
  • Create New...