简体   繁体   中英

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.

你可以使用 instrrev

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM