Find file name from full file path

FileRPath

File Problem Overview


Is there a way to extract the file name from the file full path (part of a file path) without the hassle of manipulating string?

The equivalent in Java would be:

File f = new File ("C:/some_dir/a")
f.getName() //output a
f.getFullAbsolutePath() //output c:/some_dir/a

File Solutions


Solution 1 - File

Use

basename("C:/some_dir/a.ext")
# [1] "a.ext"
dirname("C:/some_dir/a.ext")
# [1] "C:/some_dir"

Solution 2 - File

The tidyverse equivalent lives in the fs package. {fs} makes use of libuv under the hood.

library("fs")

path_file("/some/path/to/file.xyz")
#> [1] "file.xyz"

path_dir("/some/path/to/file.xyz")
#> [1] "/some/path/to"

Created on 2020-02-19 by the reprex package (v0.3.0)

Solution 3 - File

@Honeybear. The function that removes the extension from the filename you could use is the function from the {tools} R package

tools::file_path_sans_ext("ABCD.csv")
## [1] "ABCD"

See this post in SO

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondefooView Question on Stackoverflow
Solution 1 - FilemjvView Answer on Stackoverflow
Solution 2 - Filepat-sView Answer on Stackoverflow
Solution 3 - FileMaddocentView Answer on Stackoverflow