Jump to content

Recommended Posts

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
Posted

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.

Posted

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.

Posted

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)
Posted

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.

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.
×
×
  • Create New...