Mintoris Forum

Author Topic: Filtered Dir$()  (Read 2355 times)

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Filtered Dir$()
« on: Aug 11, 2013, 04:14 AM »
Here is a handy little subroutine I wrote for someone recently. It returns a directory string array filtered by file extension.

' DirFilter Subroutine
' Written by: Charles Charbonneau
' Aug 2013

' Include "DirFilter.bas"

' d$() = DirFilter$(".zip")

' if d$(0) = "" then print "no files found"

Sub DirFilter$(ext$)

  length = len(ext$)
  d$() = Dir$()
  n() = getDim(d$())

  nbrFiles = 0
  For i = 0 to n(1)
    if lower$(right$(d$(i),length)) = lower$(ext$) then nbrFiles = nbrFiles + 1
  next

  if nbrFiles > 0 then
    dim filtered$(nbrFiles-1)
    j = 0
    For i = 0 to n(1)
      if lower$(right$(d$(i),length)) = lower$(ext$) then
        filtered$(j) = d$(i)
        j = j + 1
      endif
    next
  else
    ' No files found
    ' return a 1 element array
    ' with a single empty string
    dim filtered$(0)
  endif

  return filtered$()

End Sub
« Last Edit: Aug 11, 2013, 04:17 AM by Chuck »

mjcoon

  • Sr. Member
  • ****
  • Posts: 116
Re: Filtered Dir$()
« Reply #1 on: Aug 11, 2013, 09:24 AM »
Here is a handy little subroutine I wrote for someone recently. It returns a directory string array filtered by file extension.

' DirFilter Subroutine
' Written by: Charles Charbonneau
' Aug 2013

' Include "DirFilter.bas"

' d$() = DirFilter$(".zip")

' if d$(0) = "" then print "no files found"

Sub DirFilter$(ext$)

  length = len(ext$)
  d$() = Dir$()
  n() = getDim(d$())

  nbrFiles = 0
  For i = 0 to n(1)
    if lower$(right$(d$(i),length)) = lower$(ext$) then nbrFiles = nbrFiles + 1
  next

  if nbrFiles > 0 then
    dim filtered$(nbrFiles-1)
    j = 0
    For i = 0 to n(1)
      if lower$(right$(d$(i),length)) = lower$(ext$) then
        filtered$(j) = d$(i)
        j = j + 1
      endif
    next
  else
    ' No files found
    ' return a 1 element array
    ' with a single empty string
    dim filtered$(0)
  endif

  return filtered$()

End Sub


That looks useful. But to optimise a bit (possibly not detectably faster in practice) I would replace the two occurrences of lower$(ext$) with a local variable lowerext$ which gets set once at the top.

And perhaps to ensure that callers include the period and not an asterisk (as in a regular expression), to check the 1st character of ext$.

Mike.

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Filtered Dir$()
« Reply #2 on: Aug 11, 2013, 06:11 PM »
You don't have to add an additional variable. At the top you could just say ext$ = lower$(ext$)  Remember that a for loop can do 20000 - 25000 loops per second depending on the device. But, then again, every little bit helps.
« Last Edit: Aug 11, 2013, 06:57 PM by Chuck »

robotMeiker

  • Full Member
  • ***
  • Posts: 49
Re: Filtered Dir$()
« Reply #3 on: Aug 18, 2013, 12:22 AM »
Thanx, these additions makes it better and I will certainly add it to my current prog. For now I'm leaving it as per your first suggestion, since I'm the only one using it, but eventually I will have to share the prog when I have everything (my 3D printer) working, and this interface will make it user friendlier.

Cesar