How to get last folder in directory path?

Just starting out? Need help? Post your questions and find answers here.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

How to get last folder in directory path?

Post by Mythros »

Hi all! How do you get the last directory folder from a directory path string?

Example :

I have a folder in C:/ called 'out'

C:/out/

I want to grab 'out' without the slashes

Any & all help is both welcome & appreciated!

Thank You! <3
User avatar
Peace
User
User
Posts: 14
Joined: Sun Aug 10, 2008 12:14 pm
Location: Then
Contact:

Re: How to get last folder in directory path?

Post by Peace »

Code: Select all

Path$ = "C:/out/"

Debug GetFilePart(Trim(Path$, "/"))

i = CountString(Path$, "/")
Debug StringField(Path$, i, "/")
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: How to get last folder in directory path?

Post by Mythros »

Thank you!
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: How to get last folder in directory path?

Post by Mythros »

What if the directory as a backslash at the end without another folder after it? For some reason, the above 2 codes are returning nothing.

D:\res\src\libs\out\
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to get last folder in directory path?

Post by Little John »

Mythros wrote:What if the directory as a backslash at the end without another folder after it? For some reason, the above 2 codes are returning nothing.

D:\res\src\libs\out\
Look at Peace's two code snippets: What kind of slashes do they use? Are they using backslashes?
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: How to get last folder in directory path?

Post by Mythros »

yes, backslashes
User avatar
Peace
User
User
Posts: 14
Joined: Sun Aug 10, 2008 12:14 pm
Location: Then
Contact:

Re: How to get last folder in directory path?

Post by Peace »

Mythros wrote:What if the directory as a backslash at the end without another folder after it? For some reason, the above 2 codes are returning nothing.

D:\res\src\libs\out\
Change "/" in "\" for a real path, your first question related to a path with a "/", I simply copied your "C:/out/" ;)
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to get last folder in directory path?

Post by Little John »

Mythros wrote:yes, backslashes
False.
Peace's code snippets use forward slashes. So it shouldn't be surprising that they don't work with backslashes.
User avatar
Kiffi
Addict
Addict
Posts: 1362
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: How to get last folder in directory path?

Post by Kiffi »

<OT>

To be sure to use the correct path separator independent of the OS, you can use the constant #PS$.

Code: Select all

Debug #PS$
Debug #NPS$

Image

</OT>
Hygge
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: How to get last folder in directory path?

Post by Mythros »

Thanks guys! :)
User avatar
idle
Always Here
Always Here
Posts: 5097
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to get last folder in directory path?

Post by idle »

Kiffi wrote:<OT>

To be sure to use the correct path separator independent of the OS, you can use the constant #PS$.

Code: Select all

Debug #PS$
Debug #NPS$

Image

</OT>
now I'm going to hear guns and roses every time I use a "/" :lol:
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply