Jump to content

_IsValidFileType()


guinness
 Share

Recommended Posts

  • Replies 75
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Shaggi, what do you see wrong here when testing with a valid extension?

ScaledTest()

Func ScaledTest()
    $types = "cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;"
    $etypes = stringsplit($types,";",2)
    $path = "C:\random1\random2\random3\random4\random5\random6\random7\random8\random9\random10\random11\random12\random13\random14\random15\random16\random17\random18\random19\random20\random21\random22\random23\random24\random25\random26\random27\random28\random29\random30\"
    $ext = "wanted_file.flp" ;check last extension list, so both algorithms have to search everything through
    $path &= $ext

    ;test of new _isvalidtype
     $valid = 0
    $starttime = timerinit()
    for $i = 0 to 0xFFFF
        if _IsValidType($path,$types) then $valid += 1
    next
    $test1 = timerdiff($starttime)
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _IsValidType = ' & $test1 & @crlf) ;### Debug Console
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $valid = ' & $valid & @crlf) ;### Debug Console

    ;test of isvalidtype
     $valid = 0
    $starttime = timerinit()
    for $i = 0 to 0xFFFF
        if isvalidtype($path,$etypes) then $valid += 1
    next
    $test2 = timerdiff($starttime)
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : isvalidtype = ' & $test2 & @crlf) ;### Debug Console
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $valid = ' & $valid & @crlf) ;### Debug Console
EndFunc

func isvalidtype(byref $path, $aftypes = 0)
    if not $aftypes or not isarray($aftypes) then
        local $types[3] = ["cmd","bat","exe"]
    else
        local $types = $aftypes
    endif
    local $ipos = stringinstr($path,".",0,-1), $stype
    if not $ipos then return false
    $stype = stringmid($path,$ipos+1)
    for $ext in $types
        if $ext = $stype then
            return true
        endif
    next
    return false
endfunc

Func _IsValidType(ByRef $sFilePath, $sList = "bat;cmd;exe")
    Return StringRegExp($sFilePath, "\.(?i:\Q" & StringReplace($sList, ";", "\E|\Q", 0, 2) & "\E)\z")
EndFunc   ;==>_IsValidType
Link to comment
Share on other sites

Apply a regular expression to the list of files faster than check each file from the list. This applies to the file search.

$type1 = "zip;au3"
$path1 = "C:AutoIt3.au3"

$type2 = "zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik|mp3;wav;wma;ogg;ac3;au3"
$path2 = "C:Documents and SettingsAll UsersApplication DataMicrosoftMedia Player.au3"

$timer = TimerInit()
For $i = 1 To 10000
_IsValidFileType($path1, $type1)
Next
$timer11 = Round(TimerDiff($timer), 2) & ' msec'

$timer = TimerInit()
For $i = 1 To 10000
_IsValidFileType($path2, $type2)
Next
$timer12 = Round(TimerDiff($timer), 2) & ' msec'

; =============================================

$timer = TimerInit()
For $i = 1 To 10000
_IsValidFileType2($path1, $type1)
Next
$timer21 = Round(TimerDiff($timer), 2) & ' msec'

$timer = TimerInit()
For $i = 1 To 10000
_IsValidFileType2($path2, $type2)
Next
$timer22 = Round(TimerDiff($timer), 2) & ' msec'

; =============================================

$timer = TimerInit()
For $i = 1 To 10000
_IsValidFileType2($path1, $type1)
Next
$timer31 = Round(TimerDiff($timer), 2) & ' msec'

$timer = TimerInit()
For $i = 1 To 10000
_IsValidFileType2($path2, $type2)
Next
$timer32 = Round(TimerDiff($timer), 2) & ' msec'

MsgBox(0, '', _
'SPE1 = ' & $timer11 & @CRLF & _
'SPE2 = ' & $timer12 & @CRLF & @CRLF & _
'STR1 = ' & $timer21 & @CRLF & _
'STR2 = ' & $timer22 & @CRLF & @CRLF & _
'ARR1 = ' & $timer31 & @CRLF & _
'ARR2 = ' & $timer32)

Func _IsValidFileType($sfilepath, $slist = "bat;cmd;exe")
Return StringRegExp($sfilepath, ".(?i:Q" & StringReplace($slist, ";", "E|Q") & "E)z")
EndFunc

Func _IsValidFileType2($sfilepath, $slist = "bat;cmd;exe")
$tmp = StringInStr($sfilepath, ".", 0, -1)
If $tmp And StringInStr(';' & $slist & ';', ';' & StringTrimLeft($sfilepath, $tmp) & ';') Then Return 1
EndFunc

Func _IsValidType3($sfilepath, $slist = "bat;cmd;exe")
$slist = StringSplit($slist, ';')
Local $tmp = StringInStr($sfilepath, ".", 0, -1), $stype = StringTrimLeft($sfilepath, $tmp)
For $i = 1 To $slist[0]
If $slist[$i] = $stype Then Return 1
Next
EndFunc
Edited by AZJIO
Link to comment
Share on other sites

Shaggi, what do you see wrong here when testing with a valid extension?

ScaledTest()

Func ScaledTest()
    $types = "cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;"
    $etypes = stringsplit($types,";",2)
    $path = "C:random1random2random3random4random5random6random7random8random9random10random11random12random13random14random15random16random17random18random19random20random21random22random23random24random25random26random27random28random29random30"
    $ext = "wanted_file.flp" ;check last extension list, so both algorithms have to search everything through
    $path &= $ext

    ;test of new _isvalidtype
     $valid = 0
    $starttime = timerinit()
    for $i = 0 to 0xFFFF
        if _IsValidType($path,$types) then $valid += 1
    next
    $test1 = timerdiff($starttime)
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _IsValidType = ' & $test1 & @crlf) ;### Debug Console
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $valid = ' & $valid & @crlf) ;### Debug Console

    ;test of isvalidtype
     $valid = 0
    $starttime = timerinit()
    for $i = 0 to 0xFFFF
        if isvalidtype($path,$etypes) then $valid += 1
    next
    $test2 = timerdiff($starttime)
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : isvalidtype = ' & $test2 & @crlf) ;### Debug Console
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $valid = ' & $valid & @crlf) ;### Debug Console
EndFunc

func isvalidtype(byref $path, $aftypes = 0)
    if not $aftypes or not isarray($aftypes) then
        local $types[3] = ["cmd","bat","exe"]
    else
        local $types = $aftypes
    endif
    local $ipos = stringinstr($path,".",0,-1), $stype
    if not $ipos then return false
    $stype = stringmid($path,$ipos+1)
    for $ext in $types
        if $ext = $stype then
            return true
        endif
    next
    return false
endfunc

Func _IsValidType(ByRef $sFilePath, $sList = "bat;cmd;exe")
    Return StringRegExp($sFilePath, ".(?i:Q" & StringReplace($sList, ";", "E|Q", 0, 2) & "E)z")
EndFunc   ;==>_IsValidType

Apparantly, arrays evaluate as boolean false :)

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

Yup. That took me a solid 10 min to figure out as I was not thinking anything was wrong that part. So the fix would be:

ScaledTest()
Func ScaledTest()
    $types = "cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;cmd;bat;tiff;jpg;con;dll;exe;bat;pif;txt;xml;doc;bmp;mp3;mov;png;flp;"
    $etypes = stringsplit($types,";",2)
    $path = "C:random1random2random3random4random5random6random7random8random9random10random11random12random13random14random15random16random17random18random19random20random21random22random23random24random25random26random27random28random29random30"
    $ext = "wanted_file.flp" ;check last extension list, so both algorithms have to search everything through
    $path &= $ext
    ;test of new _isvalidtype
     $valid = 0
    $starttime = timerinit()
    for $i = 0 to 0xFFFF
        if _IsValidType($path,$types) then $valid += 1
    next
    $test1 = timerdiff($starttime)
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _IsValidType = ' & $test1 & @crlf) ;### Debug Console
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $valid = ' & $valid & @crlf) ;### Debug Console

    ;test of isvalidtype
     $valid = 0
    $starttime = timerinit()
    for $i = 0 to 0xFFFF
        if isvalidtype($path,$etypes) then $valid += 1
    next
    $test2 = timerdiff($starttime)
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : isvalidtype = ' & $test2 & @crlf) ;### Debug Console
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $valid = ' & $valid & @crlf) ;### Debug Console
EndFunc

func isvalidtype(byref $path, $aftypes = 0)
    if $aftypes = 0 or not isarray($aftypes) then
        local $types[3] = ["cmd","bat","exe"]
    else
        local $types = $aftypes
    endif
    local $ipos = stringinstr($path,".",0,-1), $stype
    if not $ipos then return false
    $stype = stringmid($path,$ipos+1)
    for $ext in $types
        if $ext = $stype then
            return true
        endif
    next
    return false
endfunc

Func _IsValidType(ByRef $sFilePath, $sList = "bat;cmd;exe")
    Return StringRegExp($sFilePath, ".(?i:Q" & StringReplace($sList, ";", "E|Q", 0, 2) & "E)z")
EndFunc   ;==>_IsValidType
Edited by Beege
Link to comment
Share on other sites

Apply a regular expression to the list of files faster than check each file from the list. This applies to the file search.

AZJIO, That can get even more faster if basic comparison option is used for stringreplace().

$type1 = "zip;au3"
$path1 = "C:AutoIt3.au3"
$type2 = "zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik|mp3;wav;wma;ogg;ac3;au3"
$path2 = "C:Documents and SettingsAll UsersApplication DataMicrosoftMedia Player.au3"

$timer = TimerInit()
For $i = 1 To 10000
     _IsValidFileType($path1, $type1)
Next
$timer11 = Round(TimerDiff($timer), 2) & ' msec'
$timer = TimerInit()
For $i = 1 To 10000
     _IsValidFileType($path2, $type2)
Next
$timer12 = Round(TimerDiff($timer), 2) & ' msec'
; =============================================
$timer = TimerInit()
For $i = 1 To 10000
     _IsValidFileType2($path1, $type1)
Next
$timer21 = Round(TimerDiff($timer), 2) & ' msec'
$timer = TimerInit()
For $i = 1 To 10000
     _IsValidFileType2($path2, $type2)
Next
$timer22 = Round(TimerDiff($timer), 2) & ' msec'
; =============================================
$timer = TimerInit()
For $i = 1 To 10000
     _IsValidType3($path1, $type1)
Next
$timer31 = Round(TimerDiff($timer), 2) & ' msec'
$timer = TimerInit()
For $i = 1 To 10000
     _IsValidType3($path2, $type2)
Next
$timer32 = Round(TimerDiff($timer), 2) & ' msec'
ConsoleWrite('SPE1 = ' & $timer11 & @CRLF & _
          'SPE2 = ' & $timer12 & @CRLF & @CRLF & _
          'STR1 = ' & $timer21 & @CRLF & _
          'STR2 = ' & $timer22 & @CRLF & @CRLF & _
          'ARR1 = ' & $timer31 & @CRLF & _
          'ARR2 = ' & $timer32 & @LF)

Func _IsValidFileType($sfilepath, $slist = "bat;cmd;exe")
     Return StringRegExp($sfilepath, ".(?i:Q" & StringReplace($slist, ";", "E|Q", 0, 2) & "E)z")
EndFunc   ;==>_IsValidFileType

Func _IsValidFileType2($sfilepath, $slist = "bat;cmd;exe")
     $tmp = StringInStr($sfilepath, ".", 0, -1)
     If $tmp And StringInStr(';' & $slist & ';', ';' & StringTrimLeft($sfilepath, $tmp) & ';') Then Return 1
EndFunc   ;==>_IsValidFileType2

Func _IsValidType3($sfilepath, $slist = "bat;cmd;exe")
     $slist = StringSplit($slist, ';')
     Local $tmp = StringInStr($sfilepath, ".", 0, -1), $stype = StringTrimLeft($sfilepath, $tmp)
     For $i = 1 To $slist[0]
          If $slist[$i] = $stype Then Return 1
     Next
EndFunc   ;==>_IsValidType3

Default Option:
SPE1 = 165.55 msec
SPE2 = 468.94 msec

Basic Option:
SPE1 = 147.75 msec
SPE2 = 264.28 msec

edit: Added third function check

Edited by Beege
Link to comment
Share on other sites

Maybe I should switch to the faster operation then!

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Beege, in your last post I believe you are missing the third function in your tests.

Edited by guinness

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Beege, in your last post I believe you are missing the third function in your tests.

Oh sorry I should have clarified when I posted those results. I got them by running two different tests and just posting them into the forum as I went along. The actually script posted wasn't meant to compare the stringreplace() options.. :)

edit:

Maybe I should switch to the faster operation then!

I want to say yes but maybe you should leave an option. The fact that stringreplace() is by default set to use the user's local makes me wonder if it could have an effect with people using Unicode. Edited by Beege
Link to comment
Share on other sites

I have the following results :

SPE1 = 129.98 msec

SPE2 = 246.42 msec

STR1 = 127.7 msec

STR2 = 185.35 msec

ARR1 = 127.76 msec

ARR2 = 186.86 msec

guinness

For what purpose is this function?

I used the analogy in _FileSearch ($TypeMask = 2), and it works slowly. This is especially noticeable when a small number of file types. If you use the full processing of the list using the SPE, it is faster.

I even wanted to do so:

$Mask='bat|cmd|exe'
$Mask='*.'&StringReplace($Mask, '|', '|*.')
MsgBox(0, '', $Mask)
and use the $TypeMask=1 instead of $TypeMask=2 Edited by AZJIO
Link to comment
Share on other sites

I have the following results :

Strange... these are my actual results: :)

SPE1 = 180.74 msec
SPE2 = 261.54 msec

STR1 = 156.42 msec
STR2 = 346.08 msec

ARR1 = 197.24 msec
ARR2 = 354.07 msec
Link to comment
Share on other sites

Ahh shit! Now I get what guinness about the third function missing! How the hell did that happend? :) Ill edit my post above. My actual results are:

SPE1 = 157.53 msec
SPE2 = 254.65 msec

STR1 = 154.19 msec
STR2 = 350.22 msec

ARR1 = 260.56 msec
ARR2 = 866.79 msec
Edited by Beege
Link to comment
Share on other sites

Hi again.

If speed is the issue for use with large loops for example a 2 step function would be better, prepare a pattern and use that over and over again instead of creating it each call.

Here is an example, using _IsValidFileTypeEx_CreateMask and _IsValidFileTypeEx_Check:

Global $fQuestion_AreYouAnImpatientPerson = False
Global $aTypeLists[4] = ["zip;au3", _
        "zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;au3", _
        "zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik;mp3;wav;wma;ogg;ac3;au3", _
        "zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik;mp3;wav;wma;ogg;ac3;zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik;mp3;wav;wma;ogg;ac3;zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik;mp3;wav;wma;ogg;ac3;zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik;mp3;wav;wma;ogg;ac3;au3"]


; the string based method wins in speed over the SRE method (for longet type lists) and is therefor more efficient for use in loops etc.
_Test(@ScriptFullPath, $aTypeLists, 1000)
; the new SRE based is even faster but only works if the mask needed is the same, like in a loop
_Test(@ScriptFullPath, $aTypeLists, 1000, True)

; What if the users decides to check for the extension 'tar.gz' and to exclude '.gz'? (string based method will fail)
;~ ConsoleWrite("str_IsValidFileType " & str_IsValidFileType(@ScriptFullPath & '.tar.gz', 'au3;bat;cmd;tar.gz') & "  @error=" & @error & "  @extended=" & @extended & @CRLF)
;~ ConsoleWrite("_IsValidFileTypeEx_Check " & _IsValidFileType(@ScriptFullPath & '.tar.gz', 'au3;bat;cmd;tar.gz') & "  @error=" & @error & "  @extended=" & @extended & @CRLF)
;~ ConsoleWrite("_IsValidFileTypeEx_Check " & _IsValidFileTypeEx_Check(@ScriptFullPath & '.tar.gz', _IsValidFileTypeEx_CreateMask('au3;bat;cmd;tar.gz')) & "  @error=" & @error & "  @extended=" & @extended & @CRLF)


Func _Test($sPath, $aTypeLists, $iLoops, $fEx = False)
    Local $aiTimer[3] = [0, 0, 0], $iItems, $sMask, $sTypeList
    Local $sResultTable = StringFormat(" %-10s %-10s %-10s  %10s %10s %10s %10s\r\n------------------------------------------------------------------------------\r\n", 'LOOPS', 'FLTR LEN', 'FLTR ITEMS', 'SRE AVG', 'STR AVG', 'DIFF MS', 'PERCENT')
    For $n = 0 To UBound($aTypeLists) - 1 Step 1
        Local $aTimer[2] = [0, 0]
        $aiTimer[2] += $iLoops
        $sTypeList = $aTypeLists[$n]

        $aTimer[0] = TimerInit()
        For $i = 1 To $iLoops
            _IsValidFileType($sPath, $sTypeList)
        Next
        $aiTimer[0] += TimerDiff($aTimer[0])
        $aTimer[0] = Round(TimerDiff($aTimer[0]), 2)

        If $fEx Then
            $sMask = _IsValidFileTypeEx_CreateMask($sTypeList)
;~           ConsoleWrite("+> MASK: " & $sMask & @CRLF)
            $aTimer[1] = TimerInit()
            For $i = 1 To $iLoops
                _IsValidFileTypeEx_Check($sPath, $sMask)
            Next
            $aiTimer[1] += TimerDiff($aTimer[1])
            $aTimer[1] = Round(TimerDiff($aTimer[1]), 2)
        Else
            $aTimer[1] = TimerInit()
            For $i = 1 To $iLoops
                str_IsValidFileType($sPath, $sTypeList)
            Next
            $aiTimer[1] += TimerDiff($aTimer[1])
            $aTimer[1] = Round(TimerDiff($aTimer[1]), 2)
        EndIf

        StringReplace($sTypeList, ';', ';')
        $iItems = @extended + 1
        If $fQuestion_AreYouAnImpatientPerson Then ConsoleWrite('-> ' & $iLoops & ' calls' & '   ' & $iItems & ' list items' & @CRLF & _
                'REGEXP: ' & $aTimer[0] & ' msec ' & @CRLF & _
                'STRING: ' & $aTimer[1] & ' msec ' & _
                Round($aTimer[0] - $aTimer[1], 2) & ' (' & Round(($aTimer[0] / ($aTimer[1] / 100)) - 100, 2) & '%)' & @CRLF)
        $sResultTable &= StringFormat(" %-10s %-10s %-10s %10s %10s %10s %10s%%\r\n", $iLoops, StringLen($sTypeList), $iItems, $aTimer[0], $aTimer[1], Round($aTimer[0] - $aTimer[1], 2), Round(($aTimer[0] / ($aTimer[1] / 100)), 2))
    Next

    $sResultTable &= StringFormat(">%-10s %-10s %-10s %10s %10s %10s %10s%%\r\n", $aiTimer[2], '-', '-', Round($aiTimer[0], 2), Round($aiTimer[1], 2), Round($aiTimer[0] - $aiTimer[1], 2), Round(($aiTimer[0] / ($aiTimer[1] / 100)), 2))
    $sResultTable &= "> Average Increase: " & Round(($aiTimer[0] / ($aiTimer[1] / 100)) - 100, 2) & "%" & @CRLF
    ConsoleWrite($sResultTable & @CRLF)
EndFunc   ;==>_Test


Func str_IsValidFileType($sFile, $sTypeList = "bat;cmd;exe")
    Local $iPos = StringInStr($sFile, ".", 0, -1)
;~     If $iPos And StringInStr(';' & $sTypeList & ';', ';' & StringTrimLeft($sFile, $iPos) & ';') Then Return 1
    If $iPos And StringInStr(';' & $sTypeList & ';', ';' & StringTrimLeft($sFile, $iPos) & ';') Then ; this seems to be parsed a little bit faster then a single line
        Return 1
    EndIf
EndFunc   ;==>str_IsValidFileType


Func _IsValidFileType($sFile, $sTypeList = "bat;cmd;exe")
    Return StringRegExp($sFile, "\.(?i:\Q" & StringReplace($sTypeList, ";", "\E|\Q", 0, 2) & "\E)\z")
EndFunc   ;==>_IsValidFileType

; Create a mask (SRE pattern) to use with _IsValidFileTypeEx_Check.
; @error 1 = $mTypeList is invalid, not an array or string
Func _IsValidFileTypeEx_CreateMask($mTypeList = 'bat;cmd;exe')
    If IsString($mTypeList) And StringStripWS($mTypeList, 8) Then $mTypeList = StringSplit($mTypeList, ';', 2) ; if thelist is a string split it into a zero-based array
    If Not IsArray($mTypeList) Or Not UBound($mTypeList) Then Return SetError(1, 0, 0) ; no array so return
    Local $sPattern = '\.(?i:'; start pattern
    For $i = 0 To UBound($mTypeList) - 1 Step 1; add every array element's value
        $sPattern &= StringRegExpReplace($mTypeList[$i], "([.+*^?$=!(){}<>\[\]\\|:-])", "\\\1") & '|' ; we only need to escape ( ) | [ { ? $ \ but we'll do all meta chars
    Next
    Return StringTrimRight($sPattern, 1) & ')\z' ; finish pattern and return
EndFunc   ;==>_IsValidFileTypeEx_CreateMask

Func _IsValidFileTypeEx_Check($sFile, $sPattern) ; just for concistency
    Return StringRegExp($sFile, $sPattern)
EndFunc   ;==>_IsValidFileTypeEx_Check

The string based method is incrementally faster depending on the list size, the _IsValidFileTypeEx* SRE based method is consistantly about 50% faster.

This were my results for the string based method compared to the SRE based method... (Average Increase: 24.4%)

LOOPS     FLTR LEN   FLTR ITEMS  SRE AVG    CMP AVG    DIFF MS    PERCENT
------------------------------------------------------------------------------
 10000    7          2           214.8   210.77    4.03  101.91%
 10000    50         12          297.61  261.72   35.89  113.71%
 10000    106       26           394.1      328.41    65.69     120%
 10000    415       101          928.49    674.13    254.36  137.73%
>40000    -       -          1834.99    1475.02  359.97   124.4%
> Average Increase: 24.4%

This were my results for the new based method compared to the SRE based method... (Average Increase: 53.45%)

LOOPS     FLTR LEN   FLTR ITEMS  SRE AVG    CMP AVG    DIFF MS    PERCENT
------------------------------------------------------------------------------
 10000    7          2           221.97    139.78     82.19   158.8%
 10000    50         12          296.98       193.12     103.86  153.78%
 10000    106       26           392.35        256.9     135.45  152.72%
 10000    415       101         925.46       607.21  318.25  152.41%
>40000    -       -            1836.74    1196.99    639.75  153.45%
> Average Increase: 53.45%

About the string method, did you consider the case of a user looking to match '.tar.gz' and exclude '.gz' for example? (example in script above)

Edit: added script

Edited by Robjong
Link to comment
Share on other sites

Hey Robjong,

I've checked but not sure if this was what you meant. They all fail like they should.

$type2 = "zip;rar;7zip;gzip;bat;cmd;exe;avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik;mp3;wav;wma;ogg;ac3;au3;tar.gz"
$path2 = "C:Documents and SettingsAll UsersApplication DataMicrosoftmedia.tar.gz"

ConsoleWrite(_IsValidFileType($path2, $type2) & @LF)
ConsoleWrite(_IsValidFileType2($path2, $type2) & @LF)
ConsoleWrite(_IsValidType3($path2, $type2) & @LF)

Func _IsValidFileType($sfilepath, $slist = "bat;cmd;exe")
     Return StringRegExp($sfilepath, ".(?i:Q" & StringReplace($slist, ";", "E|Q", 0, 2) & "E)z")
EndFunc   ;==>_IsValidFileType
Func _IsValidFileType2($sfilepath, $slist = "bat;cmd;exe")
     $tmp = StringInStr($sfilepath, ".", 0, -1)
     If $tmp And StringInStr(';' & $slist & ';', ';' & StringTrimLeft($sfilepath, $tmp) & ';') Then Return 1
EndFunc   ;==>_IsValidFileType2
Func _IsValidType3($sfilepath, $slist = "bat;cmd;exe")
     $slist = StringSplit($slist, ';')
     Local $tmp = StringInStr($sfilepath, ".", 0, -1), $stype = StringTrimLeft($sfilepath, $tmp)
     For $i = 1 To $slist[0]
          If $slist[$i] = $stype Then Return 1
     Next
EndFunc   ;==>_IsValidType3

edit: oh i get it. I fixed the code above to show what you mean. Yours is the only one that passes.

edit2: And I don't believe speed will be an issue here with guinness. Correct results are most important.

Edited by Beege
Link to comment
Share on other sites

Speed has never been an issue from the start, but if the function can be improved then I'm always interested.

guinness

For what purpose is this function?

Really? I thought you understood what the purpose was for, or is this a rhetorical question?

Ahh shit! Now I get what guinness about the third function missing!

Yeh, not the third parameter but function :)

btw: I posted a new script in my previous post.

Thanks. I'll have a look and compare results.

So which version should be used after all this testing? (This question is for Beege and Robjong.)

Edited by guinness

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I want to say yes but maybe you should leave an option. The fact that stringreplace() is by default set to use the user's local makes me wonder if it could have an effect with people using Unicode.

True, so I will add it as an optional parameter if no one has any objections.

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

If $fQuestion_AreYouAnImpatientPerson Then ConsoleWrite(

:)

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...