Xojo Conferences
XDCMay2019MiamiUSA

Suggestions for files (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: REAL world London - anyone who went - how was it ?
Next thread: Constants with Global Scope


Reading *CSV Files versus *.Txt Files   -   Claude Stone
  Suggestions for files   -   Tom Russell
   Re: Suggestions for files   -   Lars Jensen
    Re: Suggestions for files   -   Tom Russell
   Re: Suggestions for files   -   Lars Jensen
   Re: Suggestions for files   -   Arnaud Nicolet

Suggestions for files
Date: 28.11.08 16:28 (Fri, 28 Nov 2008 10:28:17 -0500)
From: Tom Russell
I am looking for some suggestions on the best way to tackle the following.

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.

What would be the best way to get a list of only these types of files?

Thanks,

Tom

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Suggestions for files
Date: 28.11.08 17:32 (Fri, 28 Nov 2008 08:32:42 -0800)
From: Lars Jensen
> 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

=3D?D3D?D3D?D3D?D3D?D3D?D3D=
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

=sDwDDoD#DwDcDkDoDD3D?D3D=
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

=D#D#DoD#DgDD#D#DgDwDoD#D3
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Suggestions for files
Date: 28.11.08 17:45 (Fri, 28 Nov 2008 11:45:31 -0500)
From: Tom Russell
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
>
> =?DGD?DGD?DGD?DGD?DGD?DGD?DD>
> 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
>
> =sD#DgDDD#DoD#DwDcDkDoD?D>>
> 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
>
> =oD#DgD?D?D#D#DoD{DSDsD?DGDp>
> _______________________________________________
> 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>

Re: Suggestions for files
Date: 28.11.08 20:08 (Fri, 28 Nov 2008 11:08:41 -0800)
From: Lars Jensen
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 <<email address removed>> 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
>>
>> =?D3D?D3D?D3D?D3D?D3D?D3D?D3>>
>> 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
>>
>> =?DGDgDGDoDwDoDD?D?D#D3D?D3>>
>> 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
>>
>> =#DgDwDoD#D3D#DoD#D#DgD+DoDW>>
>> _______________________________________________
>> 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>

Re: Suggestions for files
Date: 28.11.08 22:26 (Fri, 28 Nov 2008 22:26:47 +0100)
From: Arnaud Nicolet
Le 28 nov. 08 à 16:28 (soir), Tom Russell a écrit:

> I am looking for some suggestions on the best way to tackle the
> following.
>
> 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.
>
> What would be the best way to get a list of only these types of files?

I don't know whether it's the best way, but this is one:

dim f As FolderItem
dim g As FolderItem
dim h() As FolderItem
dim i As Integer
dim s As String
dim t As String

for i=1 to f.Count
g=f.TrueItem(i)
s=g.Name
if Left(s,3)="grp" and Right(s,4)=".xml" then
t=mid(s,4,len(s)-7)
if str(val(t))=t then
h.Append g
end if
end if
Next
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>