PDA

View Full Version : [SOLVED:] Macro: list of the 3 last subfolder of a file



Ricco
08-22-2021, 07:54 AM
Hi,
I am looking for a macro that could extract the name of the last 3 parents folders from a file.
Example: full path = c: \ document \ example \ ... \ ... \ folder1 \ folder2 \ folder3 \ myfile.docx
I would like to get: folder1 folder2 folder3

Thanks a lot by advance

Aussiebear
08-22-2021, 02:47 PM
This thread looks a little familiar Rico. Have you posted this elsewhere?

arnelgp
08-22-2021, 07:13 PM
dim path as string
dim var as variant
dim i as integer
dim j as integer
dim sReturn as string
path = "c: \ document \ example \ ...\ ...\folder1\folder2\folder3\myfile.docx"

var= split(path, "\")
for i = ubound(var)-1 to 0 step -1
j = j + 1
if j>3 then exit for
sReturn = var(i) & " " & sReturn
next
'sReturn is the string you need
sReturn = Trim$(sReturn)

Ricco
08-23-2021, 01:25 AM
Hi arnelgp,
it's perfect :clap:, have a nice day

arnelgp
08-23-2021, 02:01 AM
Hi arnelgp,
it's perfect :clap:, have a nice day
you're welcome!