Page 1 of 1

How to get last folder in directory path?

Posted: Thu Jun 04, 2020 7:00 pm
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

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 7:31 pm
by Peace

Code: Select all

Path$ = "C:/out/"

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

i = CountString(Path$, "/")
Debug StringField(Path$, i, "/")

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 8:14 pm
by Mythros
Thank you!

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 8:23 pm
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\

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 8:30 pm
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?

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 8:36 pm
by Mythros
yes, backslashes

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 8:38 pm
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/" ;)

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 8:45 pm
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.

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 9:30 pm
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>

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 10:03 pm
by Mythros
Thanks guys! :)

Re: How to get last folder in directory path?

Posted: Thu Jun 04, 2020 11:14 pm
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: