Sign in to follow this  
Followers 0
Zomp

how to check if a file matches a specified mask

7 posts in this topic

#1 ·  Posted (edited)

Which is the simple way I can check if a file matches a specified mask?

That is, I'm looking a function that works as follows:

Match("zomp.au3","*.*") -> True

Match("zomp.au3","zo*.*") -> True

Match("zomp.au3","zo*.txt") -> False

I recognize that I can write a UDF function using StringRegExp, but I wonder if there is something easier.

Thanks.

Edited by Zomp

Share this post


Link to post
Share on other sites



The key point here is that the AutoIt regexp uses unix logic, which is not the same as DOS logic. I too got caught out by this because I didn't read the instructions. ;)

* in unix regexps has a very different meaning from * in dos.

Share this post


Link to post
Share on other sites

The key point here is that the AutoIt regexp uses unix logic, which is not the same as DOS logic. I too got caught out by this because I didn't read the instructions. ;)

* in unix regexps has a very different meaning from * in dos.

Yes I know. To have a function like the one I have described one can use StringInstr or StringRegExp to "translate" wildcards ? and *. Which is the easier way to do? Is there any other alternative? This is my questions.

Share this post


Link to post
Share on other sites

Is there any other alternative?

I don't think so. I wrote a function that does this a while ago.

Func _FileMatch($sFile, $sFilter)
    Local $sChar, $sChars = '\.+[^]$(){}=!<>|:'
    For $i = 1 to StringLen($sChars)
        $sChar = StringMid($sChars, $i, 1)
        $sFilter = StringReplace($sFilter, $sChar, '\' & $sChar)
    Next
    $sFilter = StringReplace($sFilter, '?', '.{1}')
    $sFilter = StringReplace($sFilter, '*', '.*')
    Return StringRegExp($sFile, $sFilter)
EndFunc

; Examples
consolewrite(_FileMatch('file.txt', '*.txt') & @CRLF)
consolewrite(_FileMatch('file.txt', 'file.*') & @CRLF)
consolewrite(_FileMatch('file.txt', 'file.t??') & @CRLF)
consolewrite(_FileMatch('file.txt', 'file.t?x') & @CRLF)
; This is the only one I'm having a problem with that I've tested.
; ComSpec will match this, but I'm not sure the best way to get this
; function to match it.
consolewrite(_FileMatch('file.txt', 'file.txt?') & @CRLF)
consolewrite(@CRLF)
; Here's your examples:
consolewrite(_FileMatch("zomp.au3","*.*") & @CRLF)
consolewrite(_FileMatch("zomp.au3","zo*.*") & @CRLF)
consolewrite(_FileMatch("zomp.au3","zo*.txt") & @CRLF)

Share this post


Link to post
Share on other sites

I don't think so. I wrote a function that does this a while ago.

Func _FileMatch($sFile, $sFilter)

Local $sChar, $sChars = '\.+[^]$(){}=!<>|:'

For $i = 1 to StringLen($sChars)

$sChar = StringMid($sChars, $i, 1)

$sFilter = StringReplace($sFilter, $sChar, '\' & $sChar)

Next

$sFilter = StringReplace($sFilter, '?', '.{1}')

$sFilter = StringReplace($sFilter, '*', '.*')

Return StringRegExp($sFile, $sFilter)

EndFunc

Good work, thanks.

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