Check If exists a path of folders

Hi.

I’m doing a way to check if a path of folder exists.
but curiously it only works when i put only one folder, like this:

[code] DirectorioLocal = SpecialFolder.Applications.Child(“C2K”)
If Not DirectorioLocal.Exists Then
Msgbox “No Existe” //al revez con el otro

Else
  Msgbox "Existe" //al revez con el otro
End If[/code]

But If I do this:
//DirectorioLocal = SpecialFolder.Applications.Child(“C2K”).Child(“CF2”).Child(“Empresas”)
It throws me an NIL object Exception.

But the answer is why?
shouldn’t be the same check if a folder exists or several folder exists?

Thanks

if there are several intervening directories & one or more do not exist somewhere along the path then you will get a nil object exception
this occurs because at the point the directory doesn’t exist anything following it cannot be resolved

you can treat the nil object exception as though exists returned false

  try
    DirectorioLocal = SpecialFolder.Applications.Child("C2K")
  catch noe as NilObjectException // lets specifically catch the NOE as that means "does not exist as well"
      DirectorioLocal = nil
  end try
   If not (DirectorioLocal is Nil) and Not DirectorioLocal.Exists Then
      Msgbox "No Existe" //al revez con el otro
    Else
      Msgbox "Existe" //al revez con el otro
    End If

[quote=284266:@Norman Palardy]if there are several intervening directories & one or more do not exist somewhere along the path then you will get a nil object exception
this occurs because at the point the directory doesn’t exist anything following it cannot be resolved

you can treat the nil object exception as though exists returned false

try DirectorioLocal = SpecialFolder.Applications.Child("C2K") catch noe as NilObjectException // lets specifically catch the NOE as that means "does not exist as well" DirectorioLocal = nil end try If not (DirectorioLocal is Nil) and Not DirectorioLocal.Exists Then Msgbox "No Existe" //al revez con el otro Else Msgbox "Existe" //al revez con el otro End If [/quote]
THANKS Norman.

My code now is:


//Check if that Path exists
  try
    DirectorioLocal = SpecialFolder.Applications.Child("C2K").Child("CF2").Child("Empresas")
    
    
    If Not (DirectorioLocal is Nil) and DirectorioLocal.Exists Then
      Msgbox "The path exists"
      
    End If
    
    
  catch noe as NilObjectException // lets specifically catch the NOE as that means "does not exist as well"
    DirectorioLocal = nil
    Msgbox "The path not exists"

  end try

I find the above discussion slightly confusing and wonder about possible errors.

Norman’s solution included.

If not (DirectorioLocal is Nil) and Not DirectorioLocal.Exists Then Msgbox "No Existe" //al revez con el otro Else Msgbox "Existe" //al revez con el otro[/b] End If

It would seem to me that there should be an OR and not an AND in the first line

When Gerardo Garcia adapted Norm’s code, he drops the second not. This basically corrects what I see to be Norm’s error. But he also drops the Else statement

[code] If Not (DirectorioLocal is Nil) and DirectorioLocal.Exists Then
Msgbox “The path exists”

End If[/code]

My own understanding, as I try and adapt this code for my own work is that dropping the Else clause is a bad idea and a cause for code failure. The situation where DirectorioLocal is not actually Nil, but it does not exist causes a problem. You get no message at all.

I always use something like

DirectorioLocal = SpecialFolder.Applications.Child("C2K").Child("CF2").Child("Empresas") If DirectorioLocal = Nil Then Msgbox("Folder Tree is invalid") Else If DirectorioLocal.Exists Then MsgBox("Folder/file exists") Else MsgBox("Folder/file does not exist") End If

I don’t see how @Norman Palardy’s code would work as until you reference the object you won’t get a NOE, but I’m sure that’s because coding was done here rather than tested in the IDE (we’ve all done this).

Typically though you’d probably want to create the tree as necessary

[code]Dim Path() As String = Split(“C2K CF2”) ’ Note if your folder names have spaces you’ll probably want to comma separate the folders instead of the default space

DirectorioLocal = SpecialFolder.Applications ’ Set the initial folder

For Each Folder As String In Path ’ Work through the folder path creating as necessary
DirectorioLocal = DirectorioLocal.Child(Folder)
If DirectorioLocal.Exits = False Then
DirectorioLocal.CreateAsFolder
End If
Next
DirectorioLocal = DirectorioLocal.Child(“Empresas”) ’ This can’t be nil as the folder tree is valid, but it may not exist
If DirectorioLocal.Exists = False Then
// Create Folder/File As required
End If
[/code]

I have coded here as well, so “all care no responsibility” :slight_smile:

@Wayne Golding - i would add some error catching while creating a folder. In fact you can create folders everywhere, on usb-sticks etc. You never know what happens in between with the target while running the code.

Good point @Joost Rongen and so we all learn :slight_smile:

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:

  1. This is overkill
  2. This is underkill
  3. This makes no sense. The Try statements are in the wrong location
  4. 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]

A couple of thoughts.

  • For Each is not guaranteed to traverse the array in order. In practice it does, but for something like this that depends on the order of the elements in the array, it is better to use For n = 0 to ubound(pathchain)

  • You are catching the wrong exception in your Try/Catch. You will get an IOException, not a NilObjectException, if CreateAsFolder fails.

  • Setting fipathway to Nil before returning nil is a bit of overkill. Simply return nil directly; fipathway is going to be destroyed immediately anyway.

Hope that was constructive.

Twas. Exactly what I was looking for.