- Offizieller Beitrag
Hi,
beim Darstellen von Pfaden beispielsweise in Listviews, steht ja für die Pfadanzeige nur begrenzter Platz zur Verfügung.
Mit der Funktion _MakeAliasFileArray wird aus einem Array, das die vollen Dateipfade enthält, ein 2D-Array erstellt mit vollem Dateipfad und zusätzlichem Anzeigenamen (Alias) in vorgegebener maximaler Länge.
Als Alias kann wahlweise nur der Dateiname oder Pfad & Dateiname verwendet werden.
- nur Dateiname
Ist der Dateiname länger als max. Länge, wird nach dem 3. Buchstaben ausgeschnitten bis zur max. Länge. An der Schnittstelle wird "~~" eingesetzt.
- Pfad & Dateiname
Ist der Dateiname länger als max. Länge, so gilt wie oben. Allerdings wird weitere 3 (bzw. 5) Zeichen eingekürzt und in der Anzeige erscheint vor dem Dateinamen "Lw:\", befindet sich die Datei nicht im Root wird "Lw:\..." vorangestellt.
Kann die Datei in voller Länge verwendet werden, wird der Pfad, so weit wie möglich übernommen und dann mit "..." zum Dateinamen verbunden.
Hier mal im Bsp., wie von der Alias-Anzeige der kpl. Pfad per Mausklick ausgewählt wird:
Spoiler anzeigen
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <ListViewConstants.au3>
$gui = GUICreate('nur Datei')
$LV = GUICtrlCreateListView('Datei (Pfad)', 20, 20, 300, 300)
$hLV = GUICtrlGetHandle($LV)
_GUICtrlListView_SetColumnWidth($hLV, 0, $LVSCW_AUTOSIZE_USEHEADER)
$btChg = GUICtrlCreateButton('Ordner u. Pfad zeigen', 20, 350, 180, 20)
$dir = FileSelectFolder('Bitte Ordner zur Rekursiv-Auflistung wählen.', @MyDocumentsDir, 4)
If @error Then Exit MsgBox(0, 'Fehler', 'Es wurde kein Ordner ausgewählt!' & @LF & 'Programm wird beendet.')
$aFiles = _GetFilesFolder_Rekursiv($dir, -1, 0)
Global $aRet = _MakeAliasFileArray($aFiles, 0)
For $i = 0 To UBound($aRet) -1
GUICtrlCreateListViewItem($aRet[$i][1], $LV)
Next
GUISetState()
[/autoit] [autoit][/autoit] [autoit]While 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case $btChg
GUICtrlSetState($btChg, $GUI_DISABLE)
GUISetState(@SW_LOCK)
WinSetTitle($gui, '', 'Datei mit Pfad')
_GUICtrlListView_DeleteAllItems($hLV)
$aRet = _MakeAliasFileArray($aFiles)
For $i = 0 To UBound($aRet) -1
GUICtrlCreateListViewItem($aRet[$i][1], $LV)
Next
GUISetState(@SW_UNLOCK)
Case $GUI_EVENT_PRIMARYUP
$cursor = GUIGetCursorInfo()
If $cursor[4] = $LV Then
$index = _GUICtrlListView_GetSelectedIndices($hLV)
If $index >= 0 Then MsgBox(0, 'markiert', $aRet[$index][0])
EndIf
EndSwitch
WEnd
;===============================================================================
; Function Name: _MakeAliasFileArray(ByRef $aFullPath, $iBoth=1, $iStartIndex=1, $iMaxAliasLen=30)
; Description: Creates an 2D-Array with Aliasname (i.e. to show in an Listview) and full path
; Parameter(s): $aFullPath 1D-Array with full file pathes (i.e. from my UDF _GetFilesFolder_Rekursiv)
; optional: $iBoth=1 Path and filename are shown (default), 0 - only filename
; optional: $iStartIndex Index of first entry (default = 1)
; optional: $iMaxAliasLen Maximum count of chars in Alias name (minimum is 20, shorter makes not sense)
; Return Value(s): Success 2D-Array with Aliasname and full file path
; Failure set @error 1 - no array given
; 2 - $iStartIndex out of range
; Author(s): BugFix ([email='bugfix@autoit.de'][/email])
;===============================================================================
Func _MakeAliasFileArray(ByRef $aFullPath, $iBoth=1, $iStartIndex=1, $iMaxAliasLen=30)
If Not IsArray($aFullPath) Then Return SetError(1,0,0)
If $iStartIndex < 0 Then Return SetError(2,0,0)
Local $iUbound = UBound($aFullPath) -$iStartIndex
If $iUbound < 1 Then Return SetError(2,0,0)
If $iMaxAliasLen < 20 Then $iMaxAliasLen = 20
Local $aOut[$iUbound][2]
For $i = 0 To UBound($aOut) -1
$aOut[$i][0] = $aFullPath[$i+$iStartIndex]
StringRegExpReplace($aOut[$i][0], '\\', '')
Local $sub = @extended -1
Local $file = StringRight($aOut[$i][0], StringLen($aOut[$i][0])-StringInStr($aOut[$i][0], '\', 1, -1))
Local $lw = StringLeft($aOut[$i][0], 3)
Switch $iBoth
Case 0
If StringLen($file) > $iMaxAliasLen Then
$aOut[$i][1] = StringLeft($file, 3) & '~~' & StringRight($file, $iMaxAliasLen -5)
Else
$aOut[$i][1] = $file
EndIf
Case 1
If StringLen($aOut[$i][0]) <= $iMaxAliasLen Then
$aOut[$i][1] = $aFullPath[$i+$iStartIndex]
Else
If $sub > 0 Then
If StringLen($file) >= $iMaxAliasLen -6 Then
$aOut[$i][1] = $lw & '...' & StringLeft($file, 3) & '~~' & StringRight($file, $iMaxAliasLen -11)
Else
$aOut[$i][1] = StringLeft($aOut[$i][0], $iMaxAliasLen -StringLen($file) -3) & '...' & $file
EndIf
Else
$aOut[$i][1] = $lw & StringLeft($file, 3) & '~~' & StringRight($file, $iMaxAliasLen -8)
EndIf
EndIf
EndSwitch
Next
Return $aOut
EndFunc ;==>_MakeAliasFileArray
;==================================================================================================
; Function Name: _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description: Recursive listing of files and/or folders
; Parameter(s): $sPath Basicpath of listing ('.' -current path, '..' -parent path)
; $sExt Extension for file selection '*' or -1 for all (Default)
; $iDir -1 Files+Folder(Default), 0 only Files, 1 only Folder
; optional: $iRetType 0 for Array, 1 for String as Return
; optional: $sDelim Delimiter for string return
; 0 -@CRLF (Default) 1 -@CR 2 -@LF 3 -';' 4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
; Array[0] includes count of found files/folder
; Author(s): BugFix ([email='bugfix@autoit.de'][/email])
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
Global $oFSO = ObjCreate('Scripting.FileSystemObject')
Global $strFiles = ''
Switch $sDelim
Case '1'
$sDelim = @CR
Case '2'
$sDelim = @LF
Case '3'
$sDelim = ';'
Case '4'
$sDelim = '|'
Case Else
$sDelim = @CRLF
EndSwitch
If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
If $sExt = -1 Then $sExt = '*'
If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
__ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
If $iRetType = 0 Then
Local $aOut
$aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
If $aOut[1] = '' Then
ReDim $aOut[1]
$aOut[0] = 0
EndIf
Return $aOut
Else
Return StringTrimRight($strFiles, StringLen($sDelim))
EndIf
EndFunc ;==>_GetFilesFolder_Rekursiv
Func __ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
If Not IsDeclared("strFiles") Then Global $strFiles = ''
If ($Dir = -1) Or ($Dir = 0) Then
For $file In $Folder.Files
If $Ext <> '*' Then
If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
$strFiles &= $file.Path & $Delim
Else
$strFiles &= $file.Path & $Delim
EndIf
Next
EndIf
For $Subfolder In $Folder.SubFolders
If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
__ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
Next
EndFunc ;==>__ShowSubFolders