Resolving Relative File Paths
· Sep 3, 08:23 PM
The GetFolderItem function does not resolve relative paths; that is, paths like ../../tic/tac/toe . But it turns out that it is very easy to write a function that will resolve a relative path to a FolderItem. This is quite useful for console applications when, for instance, you need to resolve command-line arguments to FolderItems.
Here's the code, which has been tested in Mac OS X and Linux.
Function GetFolderItemFromRelativePath(path as String) As FolderItem dim theItem as FolderItem = SpecialFolder.CurrentWorkingDirectory if theItem is nil then return nil end if dim pathComponents() as String = Split(path, "/") for i as Integer = 0 to UBound(pathComponents) theItem = theItem.Child(pathComponents(i)) if theItem is nil then exit end if next return theItem End Function
This code depends on the following behavior.
- f.Child("") returns (a FolderItem pointing to the same item as ) f
- f.Child(".") returns f
- f.Child("..") returns f.Parent
Comment
AutoResizeTextBox Example Identifying Linux Controls From a Handle
“.” and “..” would not work on Mac OS 9. Teaching such OS-dependent ways is not good, IMO.
The empty string, however, should be doc’d, as it can be made (and actually is) portable on all systems.
— Thomas Tempelmann · Sep 10, 12:23 PM · #
Were relative paths like these ever used in Mac OS 9 development? Of course, worrying about OS 9 is pointless, since REALbasic no longer supports it.
— Charles · Oct 17, 12:24 PM · #