How to Retrieve Parent Directory for Files and Folders

How to Retrieve Parent Directory for Files and Folders

The following code will return the parent directory of a given directory , or the directory of a given filename.

Dim str as String
Dim i as Integer
str = “C:\Files\test\baby.jpg”
i = InStrRev(str, “\”)
str = Left(str, i)
MsgBox str

The output for the above code will be “C:\Files\test\”

If the input is a directory (F:\Program Files\MSN) it will return that directory’s parent (F:\Program Files\).
If the input is a file (F:\changes\MAR1\test.txt) it will return that file’s directory (F:\changes\MAR1\).

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.