Jump to content

Get filename from array recurrence


telmob
 Share

Recommended Posts

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 by telmob
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 by telmob
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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.

 

#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

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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