Forward Ed
Forward Ed

Reputation: 9874

CreateFolder Method keeps returning error 76, "Path not found", when attempting to create a new folder

I did some reading, and came up with the following code:

Function ExistingDir(ByVal strPath As String) As Boolean

Dim oFSO As Object
    If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    ExistingDir = oFSO.FolderExists(strPath)

End Function

Sub CreateDir(ByVal NewFolderPath As String)
Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    oFSO.CreateFolder (NewFolderPath) '<<=== Error line
End Sub

and they are called via the following so they are being sent the same path string:

If Not ExistingDir(ArchivePath) Then
    CreateDir ArchivePath

The first checks to see if the directory exists and the second creates the directory. I keep getting the following error when I step through the code:

enter image description here

When I look in the Locals window I can see the value for NewFolderPath is:

"C:\Users\me\Desktop\LOCAL CPS folder\Vision 2020\191007\MTOD\Archive\20200124"

I read the following questions Q1 Q2 Q3

I have tried:

oFSO.CreateFolder (TRIM(NewFolderPath))
oFSO.CreateFolder (TRIM(NewFolderPath) & "\")
oFSO.CreateFolder TRIM(NewFolderPath)
oFSO.CreateFolder TRIM(NewFolderPath) & "\"

I also found this question though not sure if it applies. I suspected it might have something to do with spaces in the path, but changing the line to:

oFSO.CreateFolder """" & NewFolderPath & """"

It produced a bad filename error.

I have full read/write access to the folder C:\Users\me\Desktop\LOCAL CPS folder\Vision 2020\191007\MTOD to the best of my knowledge. I created it to begin with. I am trying to create the sub folder \Archive\20020124. And when looking though file explorer the folder does not show in the directory so its not a case of it already existing.

What am I doing wrong for creating that folder?

Update:

I think I may have figured it out. Do I need to create the folder Archive then create the folder 20020124?

Upvotes: 2

Views: 1772

Answers (1)

Forward Ed
Forward Ed

Reputation: 9874

For those looking at this in the future, the answer is that you need to create the sub directories one at a time. The Q1 in the question has many answers showing how to do this. Also note Tim Williams confirmation that this is the correct approach and that trying to generate \Archive\Date in one step was the cause of the problem.

Upvotes: 1

Related Questions