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 11:08:41 -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=cVNYaLpRsO1dXNW7d5lB1cob1eftwPXR5UsyK3xFudI=; b=bfOpI7c15snJmUIxf7Cr/+NLizjb/mHnMs8F6PIANxiizkBq5t0kYShAnBZzJ8Ccva GBa8Je5gIVT7DHGo6H5xKCAap/OC6M5vnQNZTd5JoXfKQJSf49U34nuTuWmtNO7kzqBu 1UB0CwL+QTw1QZzJk31i+GbcT38C6Ya0QH6UQ=
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=nlsiH40HjXKy4rz+II2Wg+yr23NI//mYQElDIGpL54zKrXn3q26izmGvt2PhiX6doJ gZbUgtqAmpsQyWHJt4MTkhLS8PKAuuHvTCaEp5c5oyHUzt4EkCONVLlb+UTlsvjHVyUz gSjAMPUWVTe63igjqmeonywbd8s1DYHcp9p2c=
In-reply-to: <4930202B.4040709@stny.rr.com>
References: <49300E11.8090302@stny.rr.com> <33cbfa100811280832m754b23c7je441025c04e33959@mail.gmail.com> <4930202B.4040709@stny.rr.com>
Reply-to: REALbasic NUG <realbasic-nug@lists.realsoftware.com>
Sender: realbasic-nug-bounces@lists.realsoftware.com
Yes, this code would snag "grp_tree.xml". You could modify
String.Satisfies, or replace it with a Regex-based approach, or scan
the returned array for non-numeric characters in the filename after
"grp_".

lj

On Fri, Nov 28, 2008 at 8:45 AM, Tom Russell <linuxrox@stny.rr.com> wrote:
> I havent tried this yet but I assume it would snag files such as
> grp_tree.xml as well. Like I said the folder contains other files too, so I
> have to be somewhat specific to get the files I really want.
>
> Thanks for the ideas.
>
> Lars Jensen wrote:
>>>
>>> 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>
>>
>>
>
>
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
>
> Search the archives:
> <http://support.realsoftware.com/listarchives/lists.html>
>

_______________________________________________
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>