realbasic-nug
[Top] [All Lists]

Re: Suggestions for files

To: "REALbasic NUG" <realbasic-nug@lists.realsoftware.com>
Subject: Re: Suggestions for files
From: "Lars Jensen" <larsjensen@gmail.com>
Date: Fri, 28 Nov 2008 08:32:42 -0800
Authentication-results: mx.google.com; spf=neutral (google.com: 74.124.194.228 is neither permitted nor denied by best guess record for domain of realbasic-nug-bounces@lists.realsoftware.com) smtp.mail=realbasic-nug-bounces@lists.realsoftware.com; dkim=neutral (body hash did not verify) header.i=@gmail.com
Delivered-to: listarchive@realsoftware.com
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=J8dQ8wBsXqgeooDbUjsGc4PRgdrzloLsyz09bCJ6iJU=; b=LX412T2Bb/XAd+4F9J0Ri18Bm8TGnXx3bYYqh6JG5MsryIOVyuJwUsfdmX0LE8i5Wx p+ViVmity7hBeSIw0IOmDMq3NaGlLMvYtsyYujcFMMDOmPbavdYOY5GCs8kNMWpXb/fD 08hg0MiEU0lKi06cM2T8cfWgCOWJb4U/t/SJo=
Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=T7dcfu5PoIhipfXFgv7upsDFhAVzfXQbMXD4a6+vD/ZR9qUu9dbeEovDoQnIWPilhf 4sAc/34TRIPez2t372Pr4qxelaPJvtBkEO6MNdv6R9YmgWQvmihjgMxixAmEoaGzeWom 9mVwTQJPMVuLfKQtcaeh/kNNI/hdwMIK93RS4=
In-reply-to: <49300E11.8090302@stny.rr.com>
References: <49300E11.8090302@stny.rr.com>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com
> 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>


<Prev in Thread] Current Thread [Next in Thread>