I have a folder with different files in it. I am looking only for files that
look like this:
grp1.xml
grp2.xml
grp3.xml
The first 3 letters will always be the same but the number following it will be
unknown to me.
This can be interpreted a few ways, but here is one solution:
Dim MyFiles() as FolderItem = MyParentFolder.Items(false, "grp*.xml")
The FolderItem.Items function is below, and it relies on the
String.Satisfies function, also below.
lj
=============================
Function Items(extends f as folderItem, typeIsFolder as boolean,
specs() as string) As FolderItem()
// Returns an array containing all existing items
// (non-recursive) within the given folder item that
// are not hidden, and that are of the indicated type:
// typeIsFolder = true means that only folders will be
// returned; false means that only files will be returned.
//
// Specs is an array of file spec strings, each of
// which can contain wildcards; e.g. "*.txt". If this
// array is not empty, then a folderItem must match
// one or more spec strings in order to be returned.
//
// Returns an empty array if f doesn't exist or is not
// a directory.
dim child, ret() as FolderItem
dim i as integer
dim spec as string
dim haveSpecs as boolean
dim specCount as integer
if f.Exists and f.Directory then
specCount = UBound(specs)
haveSpecs = (specCount >= 0)
for i = 1 to f.Count
child = f.TrueItem(i) // note: not f.Item(i)
if child <> nil and child.Exists _
and (not child.IsHidden) _
and (typeIsFolder = child.Directory) _
then
if haveSpecs then
for each spec in specs
if child.Name.Satisfies(spec) then
ret.Append child
end if
next spec
else // no specs?
ret.Append child // then we'll take anything
end if
end if
next i
end if
return ret
End Function
=============================
Function Satisfies(extends s as string, spec as string) As boolean
// Returns true if the input string satisfies the spec
// string, which can contain wildcard characters.
// The match is case-insensitive.
//
// A common use of this is to test filenames:
//
// dim s as string = "test.txt"
// if s.Satisfies("*.txt") then
// MsgBox "This is a text file."
// end if
//
// Note that this kind of wildcarding is quite different
// from RegEx matching (see), which is much more flexible
// and powerful, but harder to learn.
if spec = "" then // do this trivial case first
return (s = "")
end if
dim ret as boolean = true // be optimistic
const wildcard = "*"
dim wildcardLen as integer = Len(wildcard)
dim startsWild as boolean = (spec.Left(wildcardLen) = wildcard)
dim endsWild as boolean = (spec.Right(wildcardLen) = wildcard)
// Extract the non-wildcard parts of the spec,
// eliminating any empty substrings.
dim nonWildcards() as string = Split(spec, wildcard)
dim i as integer
for i = UBound(nonWildcards) downTo 0
if nonWildcards(i).Len = 0 then
nonWildcards.Remove i
end if
next i
// For a match to occur, each non-wildcard substring
// must occur within the original string, and they must
// all occur in order.
dim pos as integer
dim nextPos as integer = 1
for i = 0 to UBound(nonWildcards)
pos = s.InStr(nextPos, nonWildcards(i))
if pos = 0 then
ret = false
exit
end if
// Further, if the spec doesn't begin with a wildcard,
// then the first non-wildcard substring must start the
// string, and similarly at the other end.
if not startsWild and i = 0 and pos <> 1 then
ret = false
exit
end if
if not endsWild and i = UBound(nonWildcards) _
and pos <> s.Len - nonWildcards(i).Len + 1 then
ret = false
exit
end if
nextPos = pos + Len(nonWildcards(i))
next i
return ret
End Function
=============================
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>