Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Walking a file system tree? (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: Re: What is the best way to keep two directories in sync? (on Windows)
Next thread: listbox sorting


Re: REAL and InnoSetup question   -   Garth Hjelte
  Walking a file system tree?   -   Jim Wagner
   Re: Walking a file system tree?   -   devmllst yahoo.de
   Re: Walking a file system tree?   -   Terry Ford
   Re: Walking a file system tree?   -   wagnerj proaxis.com
   Re: Walking a file system tree?   -   Charles Yeomans
   Re: Walking a file system tree?   -   Norman Palardy
   Re: Walking a file system tree?   -   lists mgreg.com
   Re: Walking a file system tree?   -   Navdeep Bains
   Re: Walking a file system tree?   -   Jim Wagner
   Re: Walking a file system tree?   -   Terry Ford
   Re: Walking a file system tree?   -   Lars Jensen
   Re: Walking a file system tree?   -   Jim Wagner
   Re: Walking a file system tree?   -   Philip Regan

Walking a file system tree?
Date: 02.08.10 15:16 (Mon, 2 Aug 2010 07:16:33 -0700)
From: Jim Wagner
Greets, everyone -

Can someone point me toward an example of walking a file system tree?
I am particularly puzzled about dealing with the indeterminate depth
of the tree.

Many thanks!

Jim Wagner
Oregon Research Electronics

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

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

Re: Walking a file system tree?
Date: 02.08.10 23:06 (Tue, 3 Aug 2010 00:06:41 +0200)
From: devmllst yahoo.de
I have written one that does no recursion. It does not preserve the directory structure - it only returns EITHER files OR folders.
The options are self-explanatory, I hope. :-)

Function Fi_FolderItems_getAll(Extends f As FolderItem, filesOrFolders As String = "files", SubfolderContentsF As Boolean = true, withDotFiles As Boolean = false) As FolderItem()

Dim finalFileArr() As FolderItem
Dim finalFolderArr() As FolderItem

Dim foldersToInspectArr(0) As FolderItem
Dim currentFolder As FolderItem
Dim currentItem as FolderItem
Dim i, itemCount As Integer

foldersToInspectArr(0) = f
While foldersToInspectArr.Ubound >= 0
currentFolder = foldersToInspectArr.Pop

finalFolderArr.Append currentFolder

itemCount = currentFolder.Count
For i = 1 To itemCount
currentItem = (currentFolder.TrueItem(i))
If not (currentItem.Directory) Then
If Not withDotFiles And (currentItem.Name.StringStartsWith(".")) Then _
Continue
finalFileArr.Append currentItem
elseIf SubfolderContentsF Then
foldersToInspectArr.Append currentItem
End
Next
Wend

If filesOrFolders = "folders" Then
Return finalFolderArr
Else
Return finalFileArr
End
End Function


Kind regards
Christian

Re: Walking a file system tree?
Date: 02.08.10 17:54 (Mon, 2 Aug 2010 09:54:21 -0700)
From: Terry Ford

On Aug 2, 2010, at 9:43 AM, <email address removed> wrote:

> Thanks for the suggestions. I've always shied away from recursion - I
> guess from the warnings (horror stories?) embedded in that first c
> programming coarse I took., I will give this a try. And, take a look at
> the other examples suggested.

As long as the nested folders aren't large or many, you are pretty safe. There always a possibility of a stack overload in some circumstances.

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

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

Re: Walking a file system tree?
Date: 02.08.10 17:43 (Mon, 2 Aug 2010 09:43:58 -0700 (PDT))
From: wagnerj proaxis.com
>
> On Aug 2, 2010, at 10:16 AM, Jim Wagner wrote:
>
>> Greets, everyone -
>>
>> Can someone point me toward an example of walking a file system
>> tree? I am particularly puzzled about dealing with the indeterminate
>> depth of the tree.
>>
> Sub Append(extends f() as FolderItem, g() as FolderItem)
> for i as Integer = 0 to UBound(g)
> f.Append g(i)
> next
> End Sub
>
> Function TrueItems(extends f as FolderItem) as FolderItem()
> dim itemList() as FolderItem
> dim lastIndex as Integer = f.Count
> for i as Integer = 1 to lastIndex
> dim item as FolderItem = f.TrueItem(i)
> if item <> nil then
> itemList.Append item
> end if
> next
> return itemList
> End Function
>
> Function TrueDescendants(extends f as FolderItem) as FolderItem()
> for i as Integer = 0 to UBound(f.TrueItems())
> itemList.Append item
> if f.Directory then
> itemList.Append item.TrueDescendants //here be recursion
> end if
> next
> return itemList
> End Function
>
> Charles Yeomans

Thanks for the suggestions. I've always shied away from recursion - I
guess from the warnings (horror stories?) embedded in that first c
programming coarse I took., I will give this a try. And, take a look at
the other examples suggested.

Jim Wagner

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

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

Re: Walking a file system tree?
Date: 02.08.10 17:02 (Mon, 2 Aug 2010 12:02:20 -0400)
From: Charles Yeomans

On Aug 2, 2010, at 10:16 AM, Jim Wagner wrote:

> Greets, everyone -
>
> Can someone point me toward an example of walking a file system
> tree? I am particularly puzzled about dealing with the indeterminate
> depth of the tree.
>

Sub Append(extends f() as FolderItem, g() as FolderItem)
for i as Integer = 0 to UBound(g)
f.Append g(i)
next
End Sub


Function TrueItems(extends f as FolderItem) as FolderItem()
dim itemList() as FolderItem
dim lastIndex as Integer = f.Count
for i as Integer = 1 to lastIndex
dim item as FolderItem = f.TrueItem(i)
if item <> nil then
itemList.Append item
end if
next
return itemList
End Function


Function TrueDescendants(extends f as FolderItem) as FolderItem()
for i as Integer = 0 to UBound(f.TrueItems())
itemList.Append item
if f.Directory then
itemList.Append item.TrueDescendants //here be recursion
end if
next
return itemList
End Function



Charles Yeomans

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

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

Re: Walking a file system tree?
Date: 02.08.10 16:35 (Mon, 2 Aug 2010 09:35:48 -0600)
From: Norman Palardy

On Aug 2, 2010, at 8:16 AM, Jim Wagner wrote:

> Greets, everyone -
>
> Can someone point me toward an example of walking a file system
> tree? I am particularly puzzled about dealing with the indeterminate
> depth of the tree.

http://www.great-white-software.com/Great_White_Software/REALbasic_Code.html
Look for Walk Dir on the page

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

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

Re: Walking a file system tree?
Date: 02.08.10 16:31 (Mon, 2 Aug 2010 11:31:50 -0400)
From: lists mgreg.com
If you're planning to use strict RB code (i.e. no declares, etc.) I'd recommend use of "TrueChild" to ensure you're not jumping outside the directory, or worse, recursing. Just my 2¢

Best,
Michael

On Aug 2, 2010, at 10:16 AM, Jim Wagner wrote:

> Greets, everyone -
>
> Can someone point me toward an example of walking a file system tree? I am particularly puzzled about dealing with the indeterminate depth of the tree.
>
> Many thanks!
>
> Jim Wagner
> Oregon Research Electronics
>
> _______________________________________________
> 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: Walking a file system tree?
Date: 02.08.10 16:13 (Mon, 2 Aug 2010 08:13:05 -0700)
From: Navdeep Bains
>>> Can someone point me toward an example of walking a file system tree? I am particularly puzzled about dealing with the indeterminate depth of the tree.
>>
>> You could look at the CopyFileorFolder method example in the OLR's Folderitem Class for an example of recursion

If this is for a Mac application and you're comfortable with declares, you'll get much better performance out of the UNIX "fts" functions.

Type "man fts" in Terminal for more information.

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

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

Re: Walking a file system tree?
Date: 02.08.10 15:42 (Mon, 2 Aug 2010 07:42:47 -0700)
From: Jim Wagner
Thanks!

Jim
On Aug 2, 2010, at 7:30 AM, Terry Ford wrote:

>
> On Aug 2, 2010, at 7:16 AM, Jim Wagner wrote:
>
>> Greets, everyone -
>>
>> Can someone point me toward an example of walking a file system
>> tree? I am particularly puzzled about dealing with the
>> indeterminate depth of the tree.
>
> You could look at the CopyFileorFolder method example in the OLR's
> Folderitem Class for an example of recursion.
>
> Terry
> _______________________________________________
> 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: Walking a file system tree?
Date: 02.08.10 15:30 (Mon, 2 Aug 2010 07:30:41 -0700)
From: Terry Ford

On Aug 2, 2010, at 7:16 AM, Jim Wagner wrote:

> Greets, everyone -
>
> Can someone point me toward an example of walking a file system tree? I am particularly puzzled about dealing with the indeterminate depth of the tree.

You could look at the CopyFileorFolder method example in the OLR's Folderitem Class for an example of recursion.

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

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

Re: Walking a file system tree?
Date: 03.08.10 15:45 (Tue, 3 Aug 2010 10:45:01 -0400)
From: Lars Jensen
[From the thread "slow directory iteration on Windows", 2008-2009:]

These functions address a couple of issues that arise when iterating
directories:

- ItemNames lets you iterate directories quickly, primarily by giving
you back item names instead of FolderItems. You can filter the
returned string array for further discrimination of items. With large
directories (hundreds of items), this is dramatically faster on
Windows than using built-in RB functions.

- IsHidden lets you determine whether a file or folder is hidden with
respect to Mac or Windows, regardless of which platform the
application is actually running on. So your Windows application can
correctly report that an item is invisible on Mac, and vice versa.
This can be useful when you are iterating folders that are shared by
both platforms. (It has been suggested that this function can be
improved by calling Mac OS Launch Services on that platform. I have
not investigated that yet.)

Here's an RB module with these functions:

http://ljensen.com/rb/FolderItemExtensions.rbo

Use the code as you like. Comments are welcome.

lj

=3D?DDwDcDkD
Function ItemNames(extends f as folderItem, ReturnFileNames as boolean
= true, ReturnFolderNames as boolean = true) As String()
// Returns an array containing the names of child items
// within the given folder item. Returns files and/or
// folders, as directed by the input booleans.
//
// Returns an empty array if f doesn't exist, or if f
// isn't a directory, or if both input booleans are false.
//
// The iteration is not recursive. On Windows, the special
// directories "." and ".." are ignored.

dim result() as string

if f.Directory then

#if TargetWin32

// On Windows, RB's f.Item(i) is slow for folders with large
// child item counts, so we use Declares on Windows instead.
// Adapted from Aaron Ballman - see http://tinyurl.com/5susum

Soft Declare Function FindFirstFileA Lib "Kernel32" (path as
CString, data as Ptr) as Integer
Soft Declare Function FindFirstFileW Lib "Kernel32" (path as
WString, data as Ptr) as Integer
Soft Declare Function FindNextFileA Lib "Kernel32" (handle as
Integer, data as Ptr) as Boolean
Soft Declare Function FindNextFileW Lib "Kernel32" (handle as
Integer, data as Ptr) as Boolean
Declare Sub FindClose Lib "Kernel32" (handle as Integer)

dim UnicodeIsAvailable as boolean System.IsFunctionAvailable("FindFirstFileW", "Kernel32")

dim ChildData as MemoryBlock // WIN32_FIND_DATA struct
dim ChildHandle as integer

if UnicodeIsAvailable then

ChildData = new MemoryBlock(592)
ChildHandle = FindFirstFileW(f.AbsolutePath + "*.*", ChildData)

else

ChildData = new MemoryBlock(318)
ChildHandle = FindFirstFileA(f.AbsolutePath + "*.*", ChildData)

end if

if ChildHandle <> -1 then

dim ChildAttrs as UInt32 // first 4 bytes of WIN32_FIND_DATA
dim ChildName as string

// Loop through remaining items in the folder.

dim FoundNextChild as Boolean

do // loop through remaining children

ChildAttrs = ChildData.UInt32Value(0)
const NameOffset = 44

if UnicodeIsAvailable then

ChildName = ChildData.WString(NameOffset)
FoundNextChild = FindNextFileW(ChildHandle, ChildData)

else

ChildName = ChildData.CString(NameOffset)
FoundNextChild = FindNextFileA(ChildHandle, ChildData)

end if

// Now that we have its name and attributes, we can decide
// whether this child should be added to our return array.

dim ChildIsFolder as boolean = (ChildAttrs and UInt32(16)) <> 0

if (ReturnFileNames and not ChildIsFolder) or _
(ReturnFolderNames and ChildIsFolder) then

if childName <> "." and childName <> ".." then
result.Append(ChildName)
end if

end if

loop until not FoundNextChild // should really test
GetLastError for ERROR_NO_MORE_FILES

FindClose ChildHandle

end if

#else

// On non-Windows systems, pure RB code seems pretty fast.

dim child as FolderItem

for i as integer = 1 to f.Count

child = f.TrueItem(i)

if (ReturnFileNames and not Child.Directory) or _
(ReturnFolderNames and Child.Directory) then
result.Append child.Name
end if

next i

#endif

end if

return result

End Function

=#DgDwDwDDoD
Function IsHidden(extends f as FolderItem, PerformMacOSXCheck as
boolean = true) As Boolean

// Returns true if the indicated item is hidden
// in the Finder on Mac OS X, or in the Explorer
// on Windows.
//
// On Windows or Mac Classic, you could test f.Visible,
// but on Mac OS X, that test isn't sufficient. There
// are two other ways to keep an item from appearing
// in the OS X Finder:
//
// 1) Start the item's name with a '.' character.
// For example, Mac OS X maintains an invisible
// file called ".DS_Store" in each folder.
//
// 2) List the item in a file named ".hidden" (which
// will itself be invisible, according to #1), in
// the same directory as the item to be hidden.
//
// There is a ".hidden" file in the root folder of
// every Mac OS X volume. Any other folder can have
// one too, but they're rare outside the root.
//
// By default, we do the more extensive check, on the
// theory that if you just want the Visible-level check,
// you can just call that. Or you can pass in False
// for PerformMacOSXCheck.

dim Visible as boolean = f.Visible
dim fHidden as folderItem
dim stream as TextInputStream
dim hiddenFiles as string
dim sep as string

// If it's invisible according to the .Visible flag,
// that's good enough on any platform. But if we're
// checking for OS X style visibility, we have to
// dig a little deeper.

if Visible then

if PerformMacOSXCheck then

if f.Name.Left(1) = "." then

Visible = false // starts with "." -> invisible

else

if f.Parent <> nil then // the root is always visible

fHidden = f.Parent.Child(".hidden")

if fHidden <> nil and fHidden.Exists and _
not fHidden.Directory then

stream = fHidden.OpenAsTextFile
if stream <> nil then

sep = chr(10)

hiddenFiles = sep + stream.ReadAll + sep
if hiddenFiles.InStr(sep + f.Name + sep) > 0 then

Visible = false // it's in our ".hidden" list

end if

end if

end if

end if

end if

end if

end if

return not Visible

End Function

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

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

Re: Walking a file system tree?
Date: 03.08.10 05:39 (Mon, 2 Aug 2010 21:39:14 -0700)
From: Jim Wagner
Thanks! I'll take a look at that. Didn't think of that archive, but
should have.

Jim
On Aug 2, 2010, at 9:06 PM, Philip Regan wrote:

> I have an open-source version of my Simple Cataloger app on the ARBP
> website that allows for both indeterminate and predetermined depths
> of traversing a tree. It does use recursion, but the only time I
> have ever heard of someone having a problem (crashing) was when they
> were trying to catalog the entire contents of their hard drive,
> including application folders. Not much I could do about that.
>
> --
> Philip Regan
> http://www.oatmealandcoffee.com
> Treasurer - Association of REALBasic Professionals
> http://www.arbp.org
> Mac OS 10.5, RB2010r2
>
> On Aug 2, 2010, at 10:16, Jim Wagner wrote:
>
>> Greets, everyone -
>>
>> Can someone point me toward an example of walking a file system
>> tree? I am particularly puzzled about dealing with the
>> indeterminate depth of the tree.
>>
>> Many thanks!
>>
>> Jim Wagner
>> Oregon Research Electronics
>>
>> _______________________________________________
>> 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: Walking a file system tree?
Date: 03.08.10 05:06 (Tue, 3 Aug 2010 00:06:34 -0400)
From: Philip Regan
I have an open-source version of my Simple Cataloger app on the ARBP website that allows for both indeterminate and predetermined depths of traversing a tree. It does use recursion, but the only time I have ever heard of someone having a problem (crashing) was when they were trying to catalog the entire contents of their hard drive, including application folders. Not much I could do about that.