From Xojo Documentation
FolderItem class objects represent files, applications, or folders. Common ways to create FolderItems are by using the Constructor, SpecialFolder along with the FolderItem.Child method or using the various FolderItem shared methods for selecting files.
Be aware that file systems on macOS and Windows are generally not case-sensitive, while file systems on Linux usually are case-sensitive. This means that if you are creating apps to deploy on Linux (including web apps deployed to Linux servers), you need to ensure that your filenames correctly match case. If you do not, you may find that files cannot be found when your app is running on Linux.
Xojo Cloud's file system is case sensitive.
Avoid invoking functions such as Count, Child and IgnoreAlias multiple times for the same target because these functions are time-intensive (especially on macOS).
If you walk directory contents, follow these rules:
- Always iterate forward, starting at index 0 and ending with the index that matches the folder's Count. If you iterate backward, it may get very slow if the directory contains a few hundred or even more items.
- If you want to recurse into subfolders, do not go depth-first. Instead, first collect all items into an array of FolderItems, then walk the array items and enter any folders you encounter.
- To delete items from a folder, follow the above rules as well. Do not be tempted to walk the directory items backwards (from Count downto 0), even if you see many recommendations for doing so. The proper way is to first collect all items in a loop into an array, then walk the array and delete the items accordingly.
Use the DriveAt function, the Parent property of the FolderItem class, and the Child method of the FolderItem class to specify pathnames. The DriveAt function returns a reference to any drive on the user's computer/device. Pass it a number that indicates the desired volume. Passing DriveAt 0 will return the boot drive. You can get the number of drives with the DriveCount function. For example, to get a FolderItem for Microsoft Word in the Program Files folder on the boot drive, you can use the following line of code (The line continuation keyword, _, is used to split the line into two printed lines).
f = FolderItem.DriveAt(0).Child("Program Files").Child("Microsoft Office"). _
The Folderitem.Constructor can be used to get a FolderItem for an item in the current folder by passing it the name of the item. For example, the following returns a FolderItem for the folder "MyTemplates" in the current folder:
If the document or folder does not exist, the Exists property of the FolderItem is False.
If you pass the empty string to FolderItem.Constructor, it returns the FolderItem for the folder that contains the application.
The Parent property of the FolderItem class enables you to navigate one level up in the hierarchy. For example, the following gives you the FolderItem for the folder that contains the folder that contains the application:
Remember, macOS is based on BSD Unix which uses "/" as the separator.
Shell Paths and Regular Paths
If you pass the optional parameter for path, you can also pass an optional second parameter indicating whether the path is a ShellPath, a "regular" path, or a path in the form of a URL. FolderItem.PathModes has three modes that you use to indicate this, Native, Shell, and URL. For example:
You cannot pass a non-absolute Shell path. Attempting to do so will result in an UnsupportedFormatException.
If you use FolderItem.PathModes.URL, the URL must begin with "file:///".
|On MacOS with Xojo 2019r1 or earlier, constructing FolderItems with a URL where the filename included a ? character would cause that character and any that followed to be stripped. This is no longer the case in Xojo 2019r2 or greater and is now consistent across all platforms.|
You can also create a FolderItem without passing any parameters. It works the same as passing an empty text string.
If a FolderItem is actually an alias to a FolderItem, the alias is automatically resolved when the FolderItem is accessed unless you use IgnoreAlias which returns the item itself, even if it is an alias. Use the Alias property to determine whether the FolderItem is an alias.
This example puts the names of all the items on the Desktop that are stored on the boot volume into ListBox1.
If desktopFolder Is Nil Then
For Each file As Folderitem In DesktopFolder.Children
If file <> Nil Then
This example uses MoveTo. The source file will be deleted and moved into the destination folder. The destination is specified as the root of Volume 0. The name of the destination file OPis the same as the source. Notice that it checks that the source file exists and the destination FolderItem is not Nil.
f = Folderitem.ShowOpenFileDialog(FileTypeGroup1.Text)
If f <> Nil Then // if the user didn't cancel..
If f.Exists Then // if it is a valid file...
g = Volume(0).Child(f.Name)
If g <> Nil Then
MessageBox("File not found!")
This example displays the Open File dialog box and lets the user choose a JPEG file that is then assigned to the Backdrop property of a Canvas control.
If f Is Nil Then
// user cancelled
Canvas1.Backdrop = Picture.Open(f)
Catch e As IOException
// unable to open picture
This example displays an open-file dialog box that lets the user select a movie. The movie is then copied into the Movie property of a MoviePlayer control.
If f <> Nil Then
Catch e As IOException
// Unable to open movie
// user cancelled
This example copies all the files in a particular folder. The following code is a button's Pressed:
origin = Folderitem.ShowSelectFolderDialog
If origin <> Nil Then
destination = Folderitem.ShowSelectFolderDialog
If destination <> Nil Then
CopyFileOrFolder(origin, destination) // See below
The CopyFileOrFolder method is as follows:
Var newFolder As FolderItem
If source.IsFolder Then // it's a folder
newFolder = destination.Child(source.Name)
For Each file As Folderitem In source.Children // go through each item
If file.isFolder Then
// it's a folder
CopyFileOrFolder(file, newFolder) // recursively call this routine passing it the folder
file.CopyFileTo(newFolder) // it's a file so copy it
Else // it's not a folder
f = New FolderItem
g = f.FromSaveInfo(f.SaveInfo(Volume(0).Child("Documents"), 0))
If g <> Nil Then
Label2.Value = g.NativePath
MessageBox("FolderItem does not exist!")