telmob Posted February 16, 2017 Posted February 16, 2017 (edited) I'm trying to get a listview of some filenames, but i can't extract the filenames from paths in an array. I need the filenames alone, without the extension. The paths are like C:\...\...\filename.txt. I've tried using stringregexp, stringinstr, etc., but it seems a bit complicated. Here's what i got so far (that retrieves only a blank list): Func Example() Local $hTimer = TimerInit() Local $aArray = _FindInFile('2016', @ScriptDir, '*.txt') ConsoleWrite(Ceiling(TimerDiff($hTimer) / 1000) & ' second(s)' & @CRLF) $listviewB = GUICtrlCreateListView(" Atualizado", 166, 5, 156, 390) For $INDEX = 1 To $aArray[0] $POINT = StringRegExp($aArray[$INDEX], "^.*\\(.*)$", 1) If $POINT <> 0 Then GUICtrlCreateListViewItem(StringLeft($aArray[$INDEX],$POINT-1),$ListViewB) EndIf $Label2 = GUICtrlCreateLabel(_GUICtrlListView_GetItemCount($ListviewB), 166, 400, 36, 17) Next Can anyone help me out please? Edited February 16, 2017 by telmob
Moderators Melba23 Posted February 16, 2017 Moderators Posted February 16, 2017 telmob, This is how I get the filename from a path: Local $sFile = "C:\Program Files\Another Dir\AutoIt3\AutoIt3.chm" ; File name w/o ext - Example returns "AutoIt3" Local $sFilenameExExt = StringRegExpReplace($sFile, "^.*\\|\..*$", "") ConsoleWrite($sFilenameExExt & @CRLF) M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
telmob Posted February 16, 2017 Author Posted February 16, 2017 (edited) Thank you Melba. That works perfect for one file. But in the array, i only get blank lines. If i use the following, i can remove the extension, but to get the filename backword is a tad difficult for me StringInStr($aArray[$INDEX],".",0,-1) Edited February 16, 2017 by telmob
Moderators Melba23 Posted February 16, 2017 Moderators Posted February 16, 2017 telmob, How about posting the content of a representative array so that we can test on some of the real data - that way we should be able to see where the problem lies. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
telmob Posted February 16, 2017 Author Posted February 16, 2017 Oh, sorry. This part of the script i got from a post here in the forum. Can't remember where exactly. The @ScriptFolder has a few txt files, and it searches for the word '2016' in the files. expandcollapse popup#include <Array.au3> #include <Constants.au3> #include <MsgBoxConstants.au3> #include <GUIConstants.au3> #include <File.au3> #include <StringConstants.au3> #include <GuiListView.au3> #include <GUIConstantsEx.au3> GUICreate("listview items", 490, 430, 100, 200, -1) GUISetState(@SW_SHOW) MakeList() Func MakeList() Local $aArray = _FindInFile('2016', @ScriptDir, '*.txt') $listviewB = GUICtrlCreateListView(" Atualizado", 166, 5, 156, 390) For $INDEX = 1 To $aArray[0] $POINT = StringInStr($aArray[$INDEX],".",0,-1) If $POINT <> 0 Then GUICtrlCreateListViewItem(StringLeft($aArray[$INDEX],$POINT-1),$ListViewB) EndIf $Label2 = GUICtrlCreateLabel(_GUICtrlListView_GetItemCount($ListviewB), 166, 400, 36, 17) Next EndFunc ;==>MakeList While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _FindInFile($sSearch, $sFilePath, $sMask = '*', $fRecursive = True, $fLiteral = Default, $fCaseSensitive = Default, $fDetail = Default) Local $sCaseSensitive = $fCaseSensitive ? '' : '/i', $sDetail = $fDetail ? '/n' : '/m', $sRecursive = ($fRecursive Or $fRecursive = Default) ? '/s' : '' If $fLiteral Then $sSearch = ' /c:' & $sSearch EndIf If $sMask = Default Then $sMask = '*' EndIf $sFilePath = StringRegExpReplace($sFilePath, '[\\/]+$', '') & '\' Local Const $aMask = StringSplit($sMask, ';') Local $iPID = 0, $sOutput = '' For $i = 1 To $aMask[0] $iPID = Run(@ComSpec & ' /c ' & 'findstr ' & $sCaseSensitive & ' ' & $sDetail & ' ' & $sRecursive & ' "' & $sSearch & '" "' & $sFilePath & $aMask[$i] & '"', @SystemDir, @SW_HIDE, $STDOUT_CHILD) ProcessWaitClose($iPID) $sOutput &= StdoutRead($iPID) Next Return StringSplit(StringStripWS(StringStripCR($sOutput), BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)), @LF) EndFunc ;==>_FindInFile
Moderators Melba23 Posted February 16, 2017 Moderators Posted February 16, 2017 telmob, This works fine for me: Func MakeList() Local $aArray = _FindInFile('2016', @ScriptDir, '*.txt') $listviewB = GUICtrlCreateListView(" Atualizado", 166, 5, 156, 390) For $INDEX = 1 To $aArray[0] $sFileName = StringRegExpReplace($aArray[$INDEX], "^.*\\|\..*$", "") GUICtrlCreateListViewItem($sFileName, $listviewB) Next $Label2 = GUICtrlCreateLabel(_GUICtrlListView_GetItemCount($listviewB), 166, 400, 36, 17) EndFunc ;==>MakeList M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
telmob Posted February 16, 2017 Author Posted February 16, 2017 (edited) Well... that looks good I noticed it doesn't work if i have file names like 'name.name.txt', as it cuts out the whole '.name.txt', but i managed to fix it with a stringtrimright (newbie of me, i know) I really need to spend some time understanding regex. Thank you so much Melba! Edited February 16, 2017 by telmob
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now