Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a file/folder_name only from a path?

Tags:

r

stringr

Unfortunately I suck at regexp. If I have a path like so:

/long/path/to/file, I just need to extact file.

If someone supplies file/ I just need file.

If someone supplies /file/, I still need just file.

I've been using stringr functions as a crutch but this seems like straight up grep territory. Help, please?

like image 712
Maiasaura Avatar asked Mar 13 '12 23:03

Maiasaura


1 Answers

If I understand correctly, you could use the basename function.

f <- "/long/path/to/file"
basename(f)
# [1] "file"
like image 193
Joshua Ulrich Avatar answered Oct 17 '22 09:10

Joshua Ulrich