回复 10# 夜猫猫
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <SendMessage.au3>
#include <array.au3>
#include <GuiImageList.au3>
#include <winapiex.au3>
Global $icon, $CKBOXE_ID[1000]
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("搜索文件测试", 564, 577, 192, 124)
$Label1 = GUICtrlCreateLabel("搜索路径:", 55, 42, 60, 20)
$Input1 = GUICtrlCreateInput("G:\网络游戏", 110, 37, 133, 20)
$Label2 = GUICtrlCreateLabel("搜索匹配:", 330, 42, 60, 20)
$Input2 = GUICtrlCreateInput("Client,Launcher,patcher,qq,online,Setup", 385, 37, 133, 20)
$Button1 = GUICtrlCreateButton("浏览", 250, 35, 50, 25)
$Button2 = GUICtrlCreateButton("搜索", 232, 95, 97, 33)
$Button3 = GUICtrlCreateButton("(全选)", 132, 537, 97, 33)
$Button4 = GUICtrlCreateButton("(添加)", 332, 537, 97, 33)
;$Checkbox1 = GUICtrlCreateCheckbox("", 0, 0, 17, 17)
$Label3 = GUICtrlCreateLabel("", 110, 70, 450, 20)
$ListView = GUICtrlCreateListView(' 选择| 主文件夹名 | 文件路径', 5, 140, 555, 390, -1, 0x00000004 + 0x00000020)
;~ _GUICtrlListView_SetExtendedListViewStyle($ListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
_GUICtrlListView_SetColumnWidth($ListView, 0, 64 + 24)
_GUICtrlListView_SetColumnWidth($ListView, 1, 100)
_GUICtrlListView_SetColumnWidth($ListView, 2, 355)
$LHand = GUICtrlGetHandle($ListView)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$file = FileSelectFolder("选择文件夹.", "")
GUICtrlSetData($Input1, $file)
Case $Button2
_SendMessage($LHand, $LVM_DELETEALLITEMS)
$filter_name_array=StringSplit(GUICtrlRead($Input2),",",2+1)
Local $filter_name=''
for $i=0 to UBound($filter_name_array)-1
$filter_name&="*"&$filter_name_array[$i]&"*.exe;"
Next
msgbox(0,0,$filter_name)
$arr = _RecFileListToArray(GUICtrlRead($Input1), $filter_name, 1, 1, 0, 2)
if Not IsArray($arr) Then ContinueCase
$filter = StringReplace(GUICtrlRead($Input2), ",", "|")
Local $image = _GUIImageList_Create(64, 64, 5, 3)
For $i = 1 To UBound($arr)-1
;~ ConsoleWrite($arr[$i] & @CRLF)
;~ If Not StringRegExp(StringRegExpReplace($arr[$i], '.+\\', ''), "(?i)^" & $filter, 0) Then
If _WinAPI_ExtractIconEx($arr[$i], -1, 0, 0, 0) > 0 Then
_GUIImageList_AddIcon($image, $arr[$i], 0, True)
Else
$hIcon = _WinAPI_ShellExtractAssociatedIcon($arr[$i], 0)
_GUIImageList_ReplaceIcon($image, -1, $hIcon)
_WinAPI_DestroyIcon($hIcon)
EndIf
;~ EndIf
Next
_GUICtrlListView_SetImageList($ListView, $image, 1)
Local $k=0
For $i = 1 To UBound($arr)-1
;~ If Not StringRegExp(StringRegExpReplace($arr[$i], '.+\\', ''), "(?i)^" & $filter, 0) Then
_GUICtrlListView_AddItem($ListView, "", $k)
;~ _GUICtrlListView_AddSubItem($ListView, $i - 1, StringRegExpReplace($arr[$i], '.+\\', ''), 1)
$path=StringRegExpReplace(GUICtrlRead($Input1), '\\', '\\\\')
$aaa="(?(?="&$path&"\\.*?\\.*)"&$path&"\\(.*?)\\.*|[a-zA-Z]:\\(.*?)\\.*)"
_GUICtrlListView_AddSubItem($ListView, $k, StringRegExpReplace($arr[$i], $aaa, '$1$2'), 1)
_GUICtrlListView_AddSubItem($ListView, $k, $arr[$i], 2)
;~ $array[$i - 1][0] = StringRegExpReplace($arr[$i], '.+\\', '')
;~ $array[$i - 1][1] = StringRegExpReplace($arr[$i], StringRegExpReplace(GUICtrlRead($Input1), '\\', '\\\\') & '\\(.*?)\\.*', '$1')
;~ $array[$i - 1][2] = $arr[$i]
$k += 1
;~ EndIf
Next
Case $Button3
_GUICtrlListView_SetItemChecked($ListView, -1)
Case $Button4
;~ MsgBox(0, "listview 项目", GUICtrlRead(GUICtrlRead($listview)), 2)
Local $jg = ""
For $i = 0 To _GUICtrlListView_GetItemCount($ListView)
If _GUICtrlListView_GetItemChecked(GUICtrlGetHandle($ListView), $i) = $GUI_CHECKED Then
$jg &= '第 ' & $i & ' 行:"' & _GUICtrlListView_GetItemText($ListView, $i, 2) & '" 已勾选' & @CRLF
EndIf
Next
MsgBox(0, '勾选的项目', $jg)
EndSwitch
WEnd
Func _RecFileListToArray($sInitialPath, $sInclude_List = "*", $iReturn = 0, $iRecur = 0, $iSort = 0, $iReturnPath = 1, $sExclude_List = "", $sExclude_List_Folder = "")
Local $asReturnList[100] = [0], $asFileMatchList[100] = [0], $asRootFileMatchList[100] = [0], $asFolderMatchList[100] = [0], $asFolderSearchList[100] = [1]
Local $sFolderSlash = "", $iMaxLevel, $sInclude_File_Mask, $sExclude_File_Mask, $sInclude_Folder_Mask = ".+", $sExclude_Folder_Mask = ":"
Local $hSearch, $fFolder, $sRetPath = "", $sCurrentPath, $sName
Local $asFolderFileSectionList[100][2] = [[0, 0]], $sFolderToFind, $iFileSectionStartIndex, $iFileSectionEndIndex
If Not FileExists($sInitialPath) Then Return SetError(1, 1, "")
If StringRight($sInitialPath, 1) = "\" Then
$sFolderSlash = "\"
Else
$sInitialPath = $sInitialPath & "\"
EndIf
$asFolderSearchList[1] = $sInitialPath
If $iRecur > 1 Or Not IsInt($iRecur) Then Return SetError(1, 4, "")
If $iRecur < 0 Then
StringReplace($sInitialPath, "\", "", 2)
$iMaxLevel = @extended - $iRecur
EndIf
If $sInclude_List = "*" Then
$sInclude_File_Mask = ".+"
Else
If Not _RFLTA_ListToMask($sInclude_File_Mask, $sInclude_List) Then Return SetError(1, 2, "")
EndIf
Switch $iReturn
Case 0
Switch $iRecur
Case 0
$sInclude_Folder_Mask = $sInclude_File_Mask
EndSwitch
Case 2
$sInclude_Folder_Mask = $sInclude_File_Mask
EndSwitch
If $sExclude_List = "" Then
$sExclude_File_Mask = ":"
Else
If Not _RFLTA_ListToMask($sExclude_File_Mask, $sExclude_List) Then Return SetError(1, 7, "")
EndIf
Switch $iReturn
Case 0
Switch $iRecur
Case 0
$sExclude_Folder_Mask = $sExclude_File_Mask
Case Else
If $sExclude_List_Folder <> "" Then
If Not _RFLTA_ListToMask($sExclude_Folder_Mask, $sExclude_List_Folder) Then Return SetError(1, 8, "")
EndIf
EndSwitch
Case 2
$sExclude_Folder_Mask = $sExclude_File_Mask
EndSwitch
If Not ($iReturn = 0 Or $iReturn = 1 Or $iReturn = 2) Then Return SetError(1, 3, "")
If Not ($iSort = 0 Or $iSort = 1 Or $iSort = 2) Then Return SetError(1, 5, "")
If Not ($iReturnPath = 0 Or $iReturnPath = 1 Or $iReturnPath = 2) Then Return SetError(1, 6, "")
While $asFolderSearchList[0] > 0
$sCurrentPath = $asFolderSearchList[$asFolderSearchList[0]]
$asFolderSearchList[0] -= 1
Switch $iReturnPath
Case 1
$sRetPath = StringReplace($sCurrentPath, $sInitialPath, "")
Case 2
$sRetPath = $sCurrentPath
EndSwitch
$hSearch = FileFindFirstFile($sCurrentPath & "*")
If $iReturn = 0 And $iSort And $iReturnPath Then
_RFLTA_AddToList($asFolderFileSectionList, $sRetPath, $asFileMatchList[0] + 1)
EndIf
If $hSearch = -1 Then
ContinueLoop
EndIf
While 1
$sName = FileFindNextFile($hSearch)
If @error Then
ExitLoop
EndIf
$fFolder = @extended
If $fFolder Then
Select
Case $iRecur < 0
StringReplace($sCurrentPath, "\", "", 0, 2)
If @extended < $iMaxLevel Then
ContinueCase
EndIf
Case $iRecur = 1
If Not StringRegExp($sName, $sExclude_Folder_Mask) Then
_RFLTA_AddToList($asFolderSearchList, $sCurrentPath & $sName & "\")
EndIf
EndSelect
EndIf
If $iSort Then
If $fFolder Then
If StringRegExp($sName, $sInclude_Folder_Mask) And Not StringRegExp($sName, $sExclude_Folder_Mask) Then
_RFLTA_AddToList($asFolderMatchList, $sRetPath & $sName & $sFolderSlash)
EndIf
Else
If StringRegExp($sName, $sInclude_File_Mask) And Not StringRegExp($sName, $sExclude_File_Mask) Then
If $sCurrentPath = $sInitialPath Then
_RFLTA_AddToList($asRootFileMatchList, $sRetPath & $sName)
Else
_RFLTA_AddToList($asFileMatchList, $sRetPath & $sName)
EndIf
EndIf
EndIf
Else
If $fFolder Then
If $iReturn <> 1 And StringRegExp($sName, $sInclude_Folder_Mask) And Not StringRegExp($sName, $sExclude_Folder_Mask) Then
_RFLTA_AddToList($asReturnList, $sRetPath & $sName & $sFolderSlash)
EndIf
Else
If $iReturn <> 2 And StringRegExp($sName, $sInclude_File_Mask) And Not StringRegExp($sName, $sExclude_File_Mask) Then
_RFLTA_AddToList($asReturnList, $sRetPath & $sName)
EndIf
EndIf
EndIf
WEnd
FileClose($hSearch)
WEnd
If $iSort Then
Switch $iReturn
Case 0
If $asRootFileMatchList[0] = 0 And $asFolderMatchList[0] = 0 Then Return SetError(1, 9, "")
Case 1
If $asRootFileMatchList[0] = 0 And $asFileMatchList[0] = 0 Then Return SetError(1, 9, "")
Case 2
If $asFolderMatchList[0] = 0 Then Return SetError(1, 9, "")
EndSwitch
Switch $iReturn
Case 2
ReDim $asFolderMatchList[$asFolderMatchList[0] + 1]
$asReturnList = $asFolderMatchList
_RFLTA_ArraySort($asReturnList)
Case 1
If $iReturnPath = 0 Then
_RFLTA_AddFileLists($asReturnList, $asRootFileMatchList, $asFileMatchList)
_RFLTA_ArraySort($asReturnList)
Else
_RFLTA_AddFileLists($asReturnList, $asRootFileMatchList, $asFileMatchList, 1)
EndIf
Case 0
If $iReturnPath = 0 Then
_RFLTA_AddFileLists($asReturnList, $asRootFileMatchList, $asFileMatchList)
$asReturnList[0] += $asFolderMatchList[0]
ReDim $asFolderMatchList[$asFolderMatchList[0] + 1]
_RFLTA_ArrayConcatenate($asReturnList, $asFolderMatchList)
_RFLTA_ArraySort($asReturnList)
Else
Local $asReturnList[$asFileMatchList[0] + $asRootFileMatchList[0] + $asFolderMatchList[0] + 1]
$asReturnList[0] = $asFileMatchList[0] + $asRootFileMatchList[0] + $asFolderMatchList[0]
_RFLTA_ArraySort($asRootFileMatchList, 1, $asRootFileMatchList[0])
For $i = 1 To $asRootFileMatchList[0]
$asReturnList[$i] = $asRootFileMatchList[$i]
Next
Local $iNextInsertionIndex = $asRootFileMatchList[0] + 1
_RFLTA_ArraySort($asFolderMatchList, 1, $asFolderMatchList[0])
For $i = 1 To $asFolderMatchList[0]
If $sFolderSlash Then
$sFolderToFind = $asFolderMatchList[$i]
Else
$sFolderToFind = $asFolderMatchList[$i] & "\"
EndIf
For $j = 1 To $asFolderFileSectionList[0][0]
If $sFolderToFind = $asFolderFileSectionList[$j][0] Then ExitLoop
Next
$iFileSectionStartIndex = $asFolderFileSectionList[$j][1]
If $j = $asFolderFileSectionList[0][0] Then
$iFileSectionEndIndex = $asFileMatchList[0]
Else
$iFileSectionEndIndex = $asFolderFileSectionList[$j + 1][1] - 1
EndIf
If $iSort = 1 Then
_RFLTA_ArraySort($asFileMatchList, $iFileSectionStartIndex, $iFileSectionEndIndex)
EndIf
$asReturnList[$iNextInsertionIndex] = $asFolderMatchList[$i]
$iNextInsertionIndex += 1
For $j = $iFileSectionStartIndex To $iFileSectionEndIndex
$asReturnList[$iNextInsertionIndex] = $asFileMatchList[$j]
$iNextInsertionIndex += 1
Next
Next
EndIf
EndSwitch
Else
If $asReturnList[0] = 0 Then Return SetError(1, 9, "")
ReDim $asReturnList[$asReturnList[0] + 1]
EndIf
Return $asReturnList
EndFunc ;==>_RecFileListToArray
Func _RFLTA_ListToMask(ByRef $sMask, $sList)
If StringRegExp($sList, "\\|/|:|\<|\>|\|") Then Return 0
$sList = StringReplace(StringStripWS(StringRegExpReplace($sList, "\s*;\s*", ";"), 3), ";", "|")
$sList = StringReplace(StringReplace(StringRegExpReplace($sList, "(\^|\$|\.)", "\\$1"), "?", "."), "*", ".*?")
$sMask = "(?i)^(" & $sList & ")\z"
Return 1
EndFunc ;==>_RFLTA_ListToMask
Func _RFLTA_AddToList(ByRef $aList, $vValue_0, $vValue_1 = -1)
If $vValue_1 = -1 Then
$aList[0] += 1
If UBound($aList) <= $aList[0] Then ReDim $aList[UBound($aList) * 2]
$aList[$aList[0]] = $vValue_0
Else
$aList[0][0] += 1
If UBound($aList) <= $aList[0][0] Then ReDim $aList[UBound($aList) * 2][2]
$aList[$aList[0][0]][0] = $vValue_0
$aList[$aList[0][0]][1] = $vValue_1
EndIf
EndFunc ;==>_RFLTA_AddToList
Func _RFLTA_AddFileLists(ByRef $asTarget, $asSource_1, $asSource_2, $iSort = 0)
ReDim $asSource_1[$asSource_1[0] + 1]
If $iSort = 1 Then _RFLTA_ArraySort($asSource_1)
$asTarget = $asSource_1
$asTarget[0] += $asSource_2[0]
ReDim $asSource_2[$asSource_2[0] + 1]
If $iSort = 1 Then _RFLTA_ArraySort($asSource_2)
_RFLTA_ArrayConcatenate($asTarget, $asSource_2)
EndFunc ;==>_RFLTA_AddFileLists
Func _RFLTA_FileListSearch(Const ByRef $avArray, $vValue, $iIndex, $sSlash)
Local $aRet[2]
If Not $sSlash Then $vValue &= "\"
For $i = $iIndex To $avArray[0]
If StringRegExpReplace($avArray[$i], "(^.*\\)(.*)", "\1") = $vValue Then ExitLoop
Next
If $i > $avArray[0] Then
If $iIndex = $avArray[0] Then $iIndex -= 1
For $i = $iIndex + 1 To 1 Step -1
If StringRegExpReplace($avArray[$i], "(^.*\\)(.*)", "\1") = $vValue Then ExitLoop
Next
If $i = 0 Then Return SetError(1, 0, "")
$aRet[1] = $i
For $i = $aRet[1] To 1 Step -1
If StringRegExpReplace($avArray[$i], "(^.*\\)(.*)", "\1") <> $vValue Then ExitLoop
Next
$aRet[0] = $i + 1
Else
$aRet[0] = $i
For $i = $aRet[0] To $avArray[0]
If StringRegExpReplace($avArray[$i], "(^.*\\)(.*)", "\1") <> $vValue Then ExitLoop
Next
$aRet[1] = $i - 1
EndIf
Return $aRet
EndFunc ;==>_RFLTA_FileListSearch
Func _RFLTA_ArraySort(ByRef $avArray, $iStart = 1, $iEnd = -99)
If $iEnd = -99 Then $iEnd = UBound($avArray) - 1
_RFLTA_QuickSort($avArray, $iStart, $iEnd)
EndFunc ;==>_RFLTA_ArraySort
Func _RFLTA_QuickSort(ByRef $avArray, ByRef $iStart, ByRef $iEnd)
Local $vTmp
If ($iEnd - $iStart) < 15 Then
Local $i, $j, $vCur
For $i = $iStart + 1 To $iEnd
$vTmp = $avArray[$i]
If IsNumber($vTmp) Then
For $j = $i - 1 To $iStart Step -1
$vCur = $avArray[$j]
If ($vTmp >= $vCur And IsNumber($vCur)) Or (Not IsNumber($vCur) And StringCompare($vTmp, $vCur) >= 0) Then ExitLoop
$avArray[$j + 1] = $vCur
Next
Else
For $j = $i - 1 To $iStart Step -1
If (StringCompare($vTmp, $avArray[$j]) >= 0) Then ExitLoop
$avArray[$j + 1] = $avArray[$j]
Next
EndIf
$avArray[$j + 1] = $vTmp
Next
Return
EndIf
Local $L = $iStart, $R = $iEnd, $vPivot = $avArray[Int(($iStart + $iEnd) / 2)], $fNum = IsNumber($vPivot)
Do
If $fNum Then
While ($avArray[$L] < $vPivot And IsNumber($avArray[$L])) Or (Not IsNumber($avArray[$L]) And StringCompare($avArray[$L], $vPivot) < 0)
$L += 1
WEnd
While ($avArray[$R] > $vPivot And IsNumber($avArray[$R])) Or (Not IsNumber($avArray[$R]) And StringCompare($avArray[$R], $vPivot) > 0)
$R -= 1
WEnd
Else
While (StringCompare($avArray[$L], $vPivot) < 0)
$L += 1
WEnd
While (StringCompare($avArray[$R], $vPivot) > 0)
$R -= 1
WEnd
EndIf
If $L <= $R Then
$vTmp = $avArray[$L]
$avArray[$L] = $avArray[$R]
$avArray[$R] = $vTmp
$L += 1
$R -= 1
EndIf
Until $L > $R
_RFLTA_QuickSort($avArray, $iStart, $R)
_RFLTA_QuickSort($avArray, $L, $iEnd)
EndFunc ;==>_RFLTA_QuickSort
Func _RFLTA_ArrayConcatenate(ByRef $avArrayTarget, Const ByRef $avArraySource)
Local $iUBoundTarget = UBound($avArrayTarget) - 1, $iUBoundSource = UBound($avArraySource)
ReDim $avArrayTarget[$iUBoundTarget + $iUBoundSource]
For $i = 1 To $iUBoundSource - 1
$avArrayTarget[$iUBoundTarget + $i] = $avArraySource[$i]
Next
EndFunc ;==>_RFLTA_ArrayConcatenate
|