Carlos Mendieta
Carlos Mendieta

Reputation: 872

Using VBA, how can I get the immediate parent folder name from a path string?

I've been given an assignment modifying some vba script and i'm unsure of how to obtain the parent folder name given a full path string.

Here's what I have so far:

'=== Required output should be "zzz"
Dim FullFolderName As String
Dim FolderName As String
Dim FullPath As String

FullPath = "x:\xxx\yyy\zzz\somefile.txt"
Dim folderobject
Set folderobject = CreateObject("Scripting.FileSystemObject")

FullFolderName = folderobject.GetParentFolderName(ThisDrawing.FullName)
'FullFolderName ends up with "x:\xxx\yyy\zzz"
'Everything above works. Below does not. I want FolderName to = "zzz"

FolderName = String.Remove(FullFolderName.LastIndexOf("\"))

So far it seems more complex than using .NET. any help is appreciated.

Upvotes: 0

Views: 1937

Answers (1)

MarcinSzaleniec
MarcinSzaleniec

Reputation: 2256

you may use instrrev

FolderName = Left(FullFolderName, InStrRev(FullFolderName, "\")-1)

Upvotes: 1

Related Questions