Any suggestions or problems post below.
Function:
; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsValidFileType ; Description ...: Checks if a filepath contains an approved filetype extension. ; Syntax ........: _IsValidFileType($sFilePath[, $sList = 'bat;cmd;exe'[, $iOpFast = 1]]) ; Parameters ....: $sFilePath - File path of the file. ; $sList - [optional] A list of filetype extensions separated with ';' e.g. 'zip;rar;7zip;gzip'. Default is 'bat;cmd;exe'. ; $iOpFast - [optional] Either 0 (No) or 1 (True) to use the faster operation for StringReplace. Default is 1. ; Return values .: Success - Return 1 = Valid filetype or 0 = InValid filetype ; Author ........: guinness ; Modified ......: ; Remarks........; The code was initially based on the original function of _RFLTA_ListToMask() by Melba23, but overtime with input from ; AZJIO, Beege and Shaggi, I opted on using Robjong's suggestion [http://www.autoitscript.com/forum/topic/123674-isvalidtype/page__view__findpost__p__959470.] ; Related .......: _WinAPI_PathMatchSpec in WinAPIEx.au3 by Yashied. ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _IsValidFileType($sFilePath, $sList = 'bat;cmd;exe', $iOpFast = 1) Return StringRegExp($sFilePath, '.(?i:Q' & StringReplace($sList, ';', 'E|Q', 0, $iOpFast * 2) & 'E)z') EndFunc ;==>_IsValidFileType
Example 1:
ConsoleWrite(_IsValidFileType('AutoIt3.cmd') & @CRLF) ; Returns 1 as .cmd has been approved as a valid filetype. ConsoleWrite(_IsValidFileType('C:AutoIt3.exe') & @CRLF) ; Returns 1 as .exe has been approved as a valid filetype. ConsoleWrite(_IsValidFileType('C:AutoIt3.zip') & @CRLF) ; Returns 0 as .zip hasn't been approved as a valid filetype. ConsoleWrite(_IsValidFileType('C:AutoIt3.zip', 'zip;rar;7zip;gzip') & @CRLF) ; Returns 1 as .zip has been approved as a valid filetype.
Example 2:
#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Example() Func Example() Local $hGUI = GUICreate('_IsValidFileType Simple Drop', 255, 155, Default, Default, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX), $WS_EX_ACCEPTFILES) Local $iLabel = GUICtrlCreateLabel('Drop Supported FileType (au3/exe/txt)', 2, 2, 250, 150, $SS_CENTER) GUICtrlSetState(-1, $GUI_DROPACCEPTED) GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $GUI_EVENT_DROPPED If _IsValidFileType(@GUI_DragFile, 'au3;exe;txt') Then GUICtrlSetData($iLabel, 'Dropped - ' & @GUI_DragFile) Else GUICtrlSetData($iLabel, 'I said ''Supported FileType!''') EndIf EndSwitch WEnd GUIDelete($hGUI) EndFunc ;==>Example
For a more complex approach with an improvement in speed then have a look at Robjong's example in post #54. It should be noted that the speed difference isn't in seconds but milli/nanoseconds.
Edited by guinness, 23 September 2012 - 09:21 PM.






