How do I extract a file/folder_name only from a path?
Question
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?
Solution
If I understand correctly, you could use the basename
function.
f <- "/long/path/to/file"
basename(f)
# [1] "file"
OTHER TIPS
What about this?
> path <- "/long/path/to/file"
> require(stringr)
> str_extract(path, "[^/]*$")
[1] "file"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow