Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

The Dir() function in VB and VBA can accept wildcard characters (* and ?) but only in the basename of the path.

Dir("path\*.txt")

is a valid request but.

Dir("path\*\*.txt")

will return Error 52 "Bad File Name or Number", because * is an invalid folder or file name. I created the ability to use wildcards in the folder names using my os os module and my List List class.

Example Call

It's not a true glob search as you can't search like "*.{cpp,hpp}", but this is far better than nothing. Example:

Debug.Print Glob("C:\users\ptwales", "????-herp\*\*.txt").ToString
' ["C:\users\ptwales\derp-herp\flerp\merp.txt", ...]

Client code must specify a starting directory. Current working directory is not implied.

Implementation

Note that os.SubItems and os.SubFolders each now return a List.

Public Function Glob(ByVal root As String, ByVal pattern As String) As List
 
 Dim pat_list As New List
 pat_list.Extend Split(pattern, os.SEP)
 
 Set Glob = GlobRecurse(root, pat_list, 1)
End Function
Private Function GlobRecurse(ByVal root As String, ByVal pat_list As List, ByVal index As Integer) As List
 
 If index = pat_list.Count Then
 Set GlobRecurse = os.SubItems(root, pat_list(index))
 Else
 
 Set GlobRecurse = New List
 
 Dim folder As Variant
 For Each folder In os.SubFolders(root, pat_list(index))
 GlobRecurse.Extend GlobRecurse(folder, pat_list, index + 1)
 Next folder
 
 End If
 
End Function

It's a bit awkward, particularly that GlobRecurse checks the pat_list.Count each iteration. It feels too much like scheme and I should be using a for loop instead.

The Dir() function in VB and VBA can accept wildcard characters (* and ?) but only in the basename of the path.

Dir("path\*.txt")

is a valid request but.

Dir("path\*\*.txt")

will return Error 52 "Bad File Name or Number", because * is an invalid folder or file name. I created the ability to use wildcards in the folder names using my os module and my List class.

Example Call

It's not a true glob search as you can't search like "*.{cpp,hpp}", but this is far better than nothing. Example:

Debug.Print Glob("C:\users\ptwales", "????-herp\*\*.txt").ToString
' ["C:\users\ptwales\derp-herp\flerp\merp.txt", ...]

Client code must specify a starting directory. Current working directory is not implied.

Implementation

Note that os.SubItems and os.SubFolders each now return a List.

Public Function Glob(ByVal root As String, ByVal pattern As String) As List
 
 Dim pat_list As New List
 pat_list.Extend Split(pattern, os.SEP)
 
 Set Glob = GlobRecurse(root, pat_list, 1)
End Function
Private Function GlobRecurse(ByVal root As String, ByVal pat_list As List, ByVal index As Integer) As List
 
 If index = pat_list.Count Then
 Set GlobRecurse = os.SubItems(root, pat_list(index))
 Else
 
 Set GlobRecurse = New List
 
 Dim folder As Variant
 For Each folder In os.SubFolders(root, pat_list(index))
 GlobRecurse.Extend GlobRecurse(folder, pat_list, index + 1)
 Next folder
 
 End If
 
End Function

It's a bit awkward, particularly that GlobRecurse checks the pat_list.Count each iteration. It feels too much like scheme and I should be using a for loop instead.

The Dir() function in VB and VBA can accept wildcard characters (* and ?) but only in the basename of the path.

Dir("path\*.txt")

is a valid request but.

Dir("path\*\*.txt")

will return Error 52 "Bad File Name or Number", because * is an invalid folder or file name. I created the ability to use wildcards in the folder names using my os module and my List class.

Example Call

It's not a true glob search as you can't search like "*.{cpp,hpp}", but this is far better than nothing. Example:

Debug.Print Glob("C:\users\ptwales", "????-herp\*\*.txt").ToString
' ["C:\users\ptwales\derp-herp\flerp\merp.txt", ...]

Client code must specify a starting directory. Current working directory is not implied.

Implementation

Note that os.SubItems and os.SubFolders each now return a List.

Public Function Glob(ByVal root As String, ByVal pattern As String) As List
 
 Dim pat_list As New List
 pat_list.Extend Split(pattern, os.SEP)
 
 Set Glob = GlobRecurse(root, pat_list, 1)
End Function
Private Function GlobRecurse(ByVal root As String, ByVal pat_list As List, ByVal index As Integer) As List
 
 If index = pat_list.Count Then
 Set GlobRecurse = os.SubItems(root, pat_list(index))
 Else
 
 Set GlobRecurse = New List
 
 Dim folder As Variant
 For Each folder In os.SubFolders(root, pat_list(index))
 GlobRecurse.Extend GlobRecurse(folder, pat_list, index + 1)
 Next folder
 
 End If
 
End Function

It's a bit awkward, particularly that GlobRecurse checks the pat_list.Count each iteration. It feels too much like scheme and I should be using a for loop instead.

example call
Source Link
cheezsteak
  • 2.4k
  • 1
  • 17
  • 33

The Dir() function in VB and VBA can accept wildcard characters (* and ?) but only in the basename of the path.

Dir("path\*.txt")

is a valid request but.

Dir("path\*\*.txt")

will return Error 52 "Bad File Name or Number", because * is an invalid folder or file name. I created the ability to use wildcards in the folder names using my os module and my List class.

Example Call

It's not a true glob search as you can't search like "*.{cpp,hpp}", but this is far better than nothing. Example:

Debug.Print Glob("C:\users\ptwales", "????-herp\*\*.txt").ToString
' ["C:\users\ptwales\derp-herp\flerp\merp.txt", ...]

Client code must specify a starting directory. Current working directory is not implied.

Implementation

Note that os.SubItems and os.SubFolders each now return a List.

Public Function Glob(ByVal root As String, ByVal pattern As String) As List
 
 Dim pat_list As New List
 pat_list.Extend Split(pattern, os.SEP)
 
 Set Glob = GlobRecurse(root, pat_list, 1)
End Function
Private Function GlobRecurse(ByVal root As String, ByVal pat_list As List, ByVal index As Integer) As List
 
 If index = pat_list.Count Then
 Set GlobRecurse = os.SubItems(root, pat_list(index))
 Else
 
 Set GlobRecurse = New List
 
 Dim folder As Variant
 For Each folder In os.SubFolders(root, pat_list(index))
 GlobRecurse.Extend GlobRecurse(folder, pat_list, index + 1)
 Next folder
 
 End If
 
End Function

It's a bit awkward, particularly that GlobRecurse checks the pat_list.Count each iteration. It feels too much like scheme and I should be using a for loop instead.

The Dir() function in VB and VBA can accept wildcard characters (* and ?) but only in the basename of the path.

Dir("path\*.txt")

is a valid request but.

Dir("path\*\*.txt")

will return Error 52 "Bad File Name or Number", because * is an invalid folder or file name. I created the ability to use wildcards in the folder names using my os module and my List class.

It's not a true glob search as you can't search like "*.{cpp,hpp}", but this is far better than nothing. Note that os.SubItems and os.SubFolders each now return a List.

Public Function Glob(ByVal root As String, ByVal pattern As String) As List
 
 Dim pat_list As New List
 pat_list.Extend Split(pattern, os.SEP)
 
 Set Glob = GlobRecurse(root, pat_list, 1)
End Function
Private Function GlobRecurse(ByVal root As String, ByVal pat_list As List, ByVal index As Integer) As List
 
 If index = pat_list.Count Then
 Set GlobRecurse = os.SubItems(root, pat_list(index))
 Else
 
 Set GlobRecurse = New List
 
 Dim folder As Variant
 For Each folder In os.SubFolders(root, pat_list(index))
 GlobRecurse.Extend GlobRecurse(folder, pat_list, index + 1)
 Next folder
 
 End If
 
End Function

It's a bit awkward, particularly that GlobRecurse checks the pat_list.Count each iteration. It feels too much like scheme and I should be using a for loop instead.

The Dir() function in VB and VBA can accept wildcard characters (* and ?) but only in the basename of the path.

Dir("path\*.txt")

is a valid request but.

Dir("path\*\*.txt")

will return Error 52 "Bad File Name or Number", because * is an invalid folder or file name. I created the ability to use wildcards in the folder names using my os module and my List class.

Example Call

It's not a true glob search as you can't search like "*.{cpp,hpp}", but this is far better than nothing. Example:

Debug.Print Glob("C:\users\ptwales", "????-herp\*\*.txt").ToString
' ["C:\users\ptwales\derp-herp\flerp\merp.txt", ...]

Client code must specify a starting directory. Current working directory is not implied.

Implementation

Note that os.SubItems and os.SubFolders each now return a List.

Public Function Glob(ByVal root As String, ByVal pattern As String) As List
 
 Dim pat_list As New List
 pat_list.Extend Split(pattern, os.SEP)
 
 Set Glob = GlobRecurse(root, pat_list, 1)
End Function
Private Function GlobRecurse(ByVal root As String, ByVal pat_list As List, ByVal index As Integer) As List
 
 If index = pat_list.Count Then
 Set GlobRecurse = os.SubItems(root, pat_list(index))
 Else
 
 Set GlobRecurse = New List
 
 Dim folder As Variant
 For Each folder In os.SubFolders(root, pat_list(index))
 GlobRecurse.Extend GlobRecurse(folder, pat_list, index + 1)
 Next folder
 
 End If
 
End Function

It's a bit awkward, particularly that GlobRecurse checks the pat_list.Count each iteration. It feels too much like scheme and I should be using a for loop instead.

Source Link
cheezsteak
  • 2.4k
  • 1
  • 17
  • 33

Glob-Like Search in VBA

The Dir() function in VB and VBA can accept wildcard characters (* and ?) but only in the basename of the path.

Dir("path\*.txt")

is a valid request but.

Dir("path\*\*.txt")

will return Error 52 "Bad File Name or Number", because * is an invalid folder or file name. I created the ability to use wildcards in the folder names using my os module and my List class.

It's not a true glob search as you can't search like "*.{cpp,hpp}", but this is far better than nothing. Note that os.SubItems and os.SubFolders each now return a List.

Public Function Glob(ByVal root As String, ByVal pattern As String) As List
 
 Dim pat_list As New List
 pat_list.Extend Split(pattern, os.SEP)
 
 Set Glob = GlobRecurse(root, pat_list, 1)
End Function
Private Function GlobRecurse(ByVal root As String, ByVal pat_list As List, ByVal index As Integer) As List
 
 If index = pat_list.Count Then
 Set GlobRecurse = os.SubItems(root, pat_list(index))
 Else
 
 Set GlobRecurse = New List
 
 Dim folder As Variant
 For Each folder In os.SubFolders(root, pat_list(index))
 GlobRecurse.Extend GlobRecurse(folder, pat_list, index + 1)
 Next folder
 
 End If
 
End Function

It's a bit awkward, particularly that GlobRecurse checks the pat_list.Count each iteration. It feels too much like scheme and I should be using a for loop instead.

lang-vb

AltStyle によって変換されたページ (->オリジナル) /