Jump to content

Check Filename


Recommended Posts

Hey all,

does any one know a function to check a filename and compare it with a filter.

For example:

_checkName("C:\hello.txt", "he*.t?t")

Func _checkName($f_FileName, $f_filter)

----
---

EndFunc

Something like wildcard checking...

I hope you could understand my "letter combination"... B)

Ghastly_MIB

Link to comment
Share on other sites

Function Reference

StringRegExp

--------------------------------------------------------------------------------

Check if a string fits a given regular expression pattern.

StringRegExp ( "test", "pattern" [, flag ])

Parameters

test The string to check

pattern The regular expression to compare.

flag [optional] A number to indicate how the function behaves. See below for details. The default is 0.

Flag Values

0 Return true/false (1/0) as to whether the test matched the pattern.

1 Return an array with the text that matched all the group patterns. Check @Extended to determine whether the pattern matched or not.

2 Same as 0.

3 Perform a global search, checking the entire string, returning an array of all results. Check @Extended to determine whether the pattern matched or not.

Return Value

Check @Error to make sure the function executed properly.

@Error Meaning

0 Executed properly. Check @Extended and/or the return value to determine if the pattern was found or not.

1 Flag invalid. Flag must be one of the numbers above. Return value is "".

2 Pattern invalid. Return value is the first location in the string that was invalid, as an integer.

@Extended is true (1) or false (0) depending if the test string matched the pattern string. The return value is dependent on the Flag value. If the flag is set for true/false (0), then the return value will match @Extended. If the flag is set for array return (1 / 3) then the following table applies.

@Extended Return value

0 (false) Match not found. Return value is "" (empty string).

1 (true) Match found. Return value is an array of all group values. If there are no groups in the pattern, the function returns "" (empty string).

Remarks

Regular expression notation is a compact way of specifying a pattern for strings that can be searched. Regular expressions are character strings in which plain text characters indicate what text should exist in the target string, and a some characters are given special meanings to indicate what variability is allowed in the target string. AutoIt regular expressions are normally case-sensitive.

Regular expressions are constructed of one or more of the following simple regular expression specifiers. If the character is not in the following table, then it will match only itself.

Repeating characters (*, +, ?, {...} ) will try to match the largest set possible, which allows the following characters to match as well, unless followed immediately by a question mark; then it will find the smallest pattern that allows the following characters to match as well.

Nested groups are allowed, but keep in mind that all the groups, except non-capturing groups, assign to the returned array, with the outer groups assigning after the inner groups.

Matching Characters

[ ... ] Match any character in the set. e.g. [aeiou] matches any lower-case vowel. A contiguous set can be defined using a dash between the starting and ending characters. e.g. [a-z] matches any lower case character. To include a dash (-) in a set, use it as the first or last character of the set. To include a closing bracket in a set, use it as the first character of the set. e.g. [][] will match either [ or ]. Note that special characters do not retain their special meanings inside a set, with the exception of \b, \n, \r, \t, and \\. \^, \- and \] match the escaped character inside a set.

[^ ... ] Match any character not in the set. e.g. [^0-9] matches any non-digit. To include a caret (^) in a set, put it after the beginning of the set or escape it (\^).

[:class:] Match a character in the given class of characters. Valid classes are: alpha (any alphabetic character), alnum (any alphanumeric character), lower (any lower-case letter), upper (any upper-case letter), digit (any decimal digit 0-9), xdigit (any hexidecimal digit, 0-9, A-F, a-f), space (any whitespace character), blank (only a space or tab), print (any printable character), graph (any printable character except spaces), cntrl (any control character [ascii 127 or <32]) or punct (any punctuation character)

[^:class:] Match any character not in the class.

( ... ) Group. The elements in the group are treated in order and can be repeated together. e.g. (ab)+ will match "ab" or "abab", but not "aba". A group will also store the text matched for use in back-references and in the array returned by the function, depending on flag value.

(?i) Case-insensitivity flag. This does not operate as a group. It tells the regular expression engine to do case-insensitive matching from that point on.

(?-i) (default) Case-sensitivity flag. This does not operate as a group. It tells the regular expression engine to do case-sensitive matching from that point on.

(?i ... ) Case-insensitive group. Behaves just like a normal group, but performs case-insensitive matches within the group.

(?-i ... ) Case-sensitive group. Behaves just like a normal group, but performs case-sensitive matches within the group. Primarily for use after (-i) flag or inside a case-insensitive group.

(?: ... ) Non-capturing group. Behaves just like a normal group, but does not record the matching characters in the array nor can the matched text be used for back-referencing.

(?i: ... ) Case-insensitive non-capturing group. Behaves just like a non-capturing group, but performs case-insensitive matches within the group.

(?-i: ... ) Case-sensitive non-capturing group. Behaves just like a non-capturing group, but performs case-sensitive matches within the group.

. Match any single character

| Or. The expression on one side or the other can be matched.

\ Escape a special character (have it match the actual character) or introduce a special character type (see below)

\\ Match an actual backslash (\)

\1 - \9 Back-reference. Match the prior group number given exactly. For example, (\a)\1 would match a double letter.

\a Match any alphabetic character (a-z, A-Z)

\A Match any alphanumeric character (a-z, A-Z, 0-9)

\b Match a backspace (chr(8))

\c? Match a control character, based on the next character. For example, \cM matches ctrl-M.

\d Match any digit (0-9)

\D Match any non-digit

\e Match an escape character (chr(27))

\l (lower-L) Match any lower-case letter (a-z)

\n Match a newline (@LF, chr(10)).

\N Match either a newline or a carriage return, but not both in sequence. Try \N? for that.

\p Match any punctuation character.

\r Match a carriage return (@CR, chr(13)).

\s Match any whitespace character: Chr(9) through Chr(13) which are Horizontal Tab, Line Feed, Vertical Tab, Form Feed, and Carriage Return, and the standard space ( Chr(32) ).

\S Match any non-whitespace character

\t Match a tab character.

\u Match any capital letter (A-Z)

\w Match any "word" character: a-z, A-Z or underscore (_)

\W Match any non-word character

\x Match any hexadecimal character.

\0### Match the ascii character whose code is given. Can be up to 3 digits.

\0x## Match the ascii character whose code is given in hexadecimal. Can be up to 2 digits.

\# Position. Record the current character location in the test string into the returned content array.

\< Match beginning of word.

\> Match end of word.

Repeating Characters

{x} Repeat the previous character, set or group exactly x times.

{x,} Repeat the previous character, set or group at least x times.

{,x} Repeat the previous character, set or group at most x times.

{x, y} Repeat the previous character, set or group between x and y times, inclusive.

* Repeat the previous character, set or group 0 or more times. Equivalent to {0,}

+ Repeat the previous character, set or group 1 or more times. Equivalent to {1,}

? The previous character, set or group may or may not appear. Equivalent to {0, 1}

? (after a repeating character) Find the smallest match instead of the largest.

"test" or "pattern" parameters cannot be a binaryString.

Related

StringInStr, StringRegExpReplace

Example

Local $sPattern, $sTest, $vResult, $nFlag

$sPattern = InputBox("StringRegExp Sample", "What is the pattern to test?")

$sTest = InputBox("StringRegExp Sample", "What is the line to test?")

$vResult = StringRegExp($sTest, $sPattern)

Select

Case @Error = 2

; Error. The pattern was invalid. $vResult = position in $sPattern where error occurred.

Case @Error = 0

if @Extended Then

; Success. Pattern matched. $vResult matches @Extended

Else

; Failure. Pattern not matched. $vResult = ""

EndIf

EndSelect

$sPattern = InputBox("StringRegExp Sample", "What is the pattern to test?")

$sTest = InputBox("StringRegExp Sample", "What is the line to test?")

$nFlag = InputBox("StringRegExp Sample", "What flag to use? 0 - true/false, 1 - single pattern array return, 3 - global pattern array return")

$vResult = StringRegExp($sTest, $sPattern, $nFlag)

Select

Case @Error = 1

; Error. Flag is bad. $vResult = ""

Case @Error = 2

; Error. The pattern was invalid. $vResult = position in $sPattern where error occurred.

Case @Error = 0

if @Extended Then

; Success. Pattern matched. $vResult has the text from the groups or true (1), depending on flag.

Else

; Failure. Pattern not matched. $vResult = "" or false (0), depending on flag.

EndIf

EndSelect

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

This is copied from the help file...

But how do I use this function?

Something like this?:

StringRegExp("C:\test.txt", "t*.t?t")

???

This wont work:

Local $sPattern, $sTest, $vResult, $nFlag

$sPattern = "test.t?t"
$sTest = "test.txxt"
$vResult = StringRegExp($sTest, $sPattern)
Select
Case @Error = 2 
   ; Error.  The pattern was invalid.  $vResult = position in $sPattern where error occurred.
    MsgBox(0, "", "Not OK")
Case @Error = 0
   if @Extended  Then
     ; Success.  Pattern matched.  $vResult matches @Extended
      MsgBox(0, "", "OK")
   Else
     ; Failure.  Pattern not matched.  $vResult = ""
      MsgBox(0, "", "Not OK")
   EndIf
EndSelect

It say's OK but it isn't

Edited by Ghastly_MIB
Link to comment
Share on other sites

Hello,

You could use RegExp but I agree that the syntax is a little hard.

To explain the Good Syntax

^T String begins with T

.* one and more char

\. the char "."

t

. one char

t

$ End of the string

HTH,

Francis

#include <Misc.au3>
$MyFile = "Test.txt"
$Good   = "^T.*\.t.t$"
$NotGood= "^A.*\.t.t$"

; First test 
Msgbox( 1 , " ", _iif(StringRegExp( $MyFile , $Good )," = " ," != " ))
; Second
Msgbox( 1 , " ", _iif(StringRegExp( $MyFile , $NotGood )," = " ," != " ))
Edited by Francis Lennert (France)
Link to comment
Share on other sites

Thanks Francis Lennert,

i chanced the script of you a "little" and I thingk it works now:

Func _FileName($f_in, $f_pattern="*.*")
Local $vResult
$f_pattern = StringReplace($f_pattern, ".", "\.")
$f_pattern = StringReplace($f_pattern, "?", ".")
$f_pattern = StringReplace($f_pattern, "*", ".*")

$f_result = StringRegExp($f_in, $f_pattern)

Select
Case @Error = 2 
   ; Error.  The pattern was invalid.  $vResult = position in $sPattern where error occurred.
    MsgBox(0, "", "Niet OK")
Case @Error = 0
   if @Extended  Then
     ; Success.  Pattern matched.  $vResult matches @Extended
      MsgBox(0, "", "OK")
   Else
     ; Failure.  Pattern not matched.  $vResult = ""
      MsgBox(0, "", "Niet OK")
   EndIf
EndSelect
EndFunc
Link to comment
Share on other sites

Hello,

I did the same Job on my own (With a Func) and a little GUI (Thanks Koda) to test the Paterns.

Have a good day,

Francis

#include <GUIConstants.au3>
#include <misc.au3>
;Generated with Form Designer preview
$Form1 = GUICreate("Test ", 374, 112, 144, 125)
$Input1 = GUICtrlCreateInput("T*T.T?T", 40, 40, 329, 21, -1, $WS_EX_CLIENTEDGE)
$Button1 = GUICtrlCreateButton("Test", 232, 72, 137, 33)
$Input2 = GUICtrlCreateInput("Test.txt", 40, 16, 121, 21, -1, $WS_EX_CLIENTEDGE)
GUICtrlCreateLabel("File :", 8, 24, 20, 17)
GUICtrlCreateLabel("Mask :", 8, 48, 30, 17)
GUISetState(@SW_SHOW)
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    case $msg = $Button1
        $File = GUICtrlRead( $Input2 )
        $Mask = GUICtrlRead( $Input1 )
        MsgBox(1," FileMask",_iif( FileMask( $File , $Mask ) , " Ok "," Not Ok" ) )
    Case Else
    ;;;;;;;
    EndSelect
WEnd
Exit

Func FileMask( $File , $Mask )

    $File=StringLower( $File )
    $Mask=StringLower( $Mask )
    $Mask=StringReplace($Mask,"*","[a-z0-9_ ]*")
    $Mask=StringReplace($Mask,"?","[a-z0-9_ ]")
    $Mask=StringReplace($Mask,".","\.")
    $Mask= ^"&$Mask&"$"

    MsgBox( 1 , "Test" , $Mask )
    
    Return StringRegExp( $File , $Mask )
;
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...