Re: Joost comment that error catching is a good idea.
I am trying to make this more concrete for me. This is a method that I developed to incorporate what I think is correct.
I am submitting this here in case anyone has comments like:
- This is overkill
- This is underkill
- This makes no sense. The Try statements are in the wrong location
- The code has the following flaws
Here is the method. It takes two parameters.
The first is a FolderItem. You would usually pass a FolderItem like that generated from SpecialFolder.Desktop or the like.
The second is an array of proposed folder names. Something like:
Dim someFolderNames(2) As String
someFolderNames(0) = "FirstLevel"
someFolderNames(1) = "SecondLevel"
someFolderNames(2) = "ThirdLevel"
The call would look like:
Dim fiPathToCreate As FolderItem
fiPathToCreate = CreateChairFolder(SpecialFolder.ApplicationData, someFolderNames())
If fiPathToCreate<> Nil Then
// We are ready to go and use the folder item that we have created
End If
[code]// METHOD_NAME:“CreateChainFolder”
// PARAMETERS: rootFolder As FolderItem, pathChain() As String
// RETURN TYPE: FolderItem
// DATE/TIME: 09/25/18 10:59
// DESCRIPTION: A pathway is entered in the form of a root folder and a chain of folder names
// The expectation is that the root folder is not Nil and it exists.
// The first parameter would generally be a SpecialFolder
// like SpecialFolder.Applications or SpecialFolder.Desktop
// If there are problems, then a Nil FolderItem is returned
Dim fiPathway As FolderItem
Dim validRoot As Boolean = False
If rootFolder <> Nil Then
If rootFolder.Exists Then
validRoot = True
End If
End If
If Not (validRoot) Then
fiPathway = Nil
Return fiPathway
End If
If uBound(pathChain) = -1 Then Return rootFolder // only a root folder provided
fiPathway = rootFolder
// Dim lastIndex As Integer = pathChain.Ubound
For Each folderName As String In pathChain
fiPathway = fiPathway.Child(folderName)
If fiPathway <> Nil Then
If Not (fiPathway.Exists) Then
Try
fiPathway.CreateAsFolder
Catch noe As NilObjectException // lets specifically catch the noe as that means "does not exist as well" (noe = nil object exception)
fiPathway = Nil // presumably unnecessary ?
Return fiPathway // some problem occurred while creating folder chain. Return a Nil FolderItem
End Try
End If
Else
Return fiPathway // some problem occurred while creating folder chain. Return a Nil FolderItem
End If
Next folderName
Return fiPathway[/code]