Sign in to follow this  
Followers 0
prazetto

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

1 post in this topic

WildcardMatch an Fast Filename Pattern Match (UDF)

Machine Code Version (x32 and x64)

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

Introduction

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.

Background

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
            General.Imports.au3
            WinAPI.Kernel32.au3
Platform  : x32 and x64 bit
Sample    : $test.au3

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

CoreFx.Wildcard.zip

Sample

$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

.

Source

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

Wikipedia

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

Share this post


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
Sign in to follow this  
Followers 0

  • Similar Content

    • drorshem
      By drorshem
      Hi,
      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 ?
    • RaiNote
      By RaiNote
      Hi,
      How to create Wildcards to search for a Filename and also for a String within a File and within the own script.
    • jguinch
      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.
    • Kidney
      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
    • Normonator
      By Normonator
      Here is a small section of my script. What I am trying to accomplish is that I am finding these files by using wildcards in fileexists(). Is there any possible way to get the filename based off of wildcards if only one file matches the criteria? Something like filegetname("1*.bkf") that returns the full file name.

      I would like this script to be able to stringtrimleft($string,1) the filename, so that I can change the first portion of file name.

      File name example:

      "0 01-31-13 16.01.20 full System Backup.bkf"
      would change to:
      "1 01-31-13 16.01.20 full System Backup.bkf"


      For $z = 5 to "0" Step -1 If FileExists($backupdest & $z & "*.bkf") Then FileMove($backupdest & $z & "*.bkf",$backupdest & ($z+1) & "*.bkf",0) EndIf
      Any help would be appreciated. Thanks.