I have a file
ABCD.csv
The length before the .csv
is not fixed and vary to any length.
How can I extract the portion
basename
will also remove the path leading to the file. And with this regex, any extension will be removed.
filepath <- "d:/Some Dir/ABCD.csv"
sub(pattern = "(.*)\\..*$", replacement = "\\1", basename(filepath))
# [1] "ABCD"
Or, using file_path_sans_ext
as Tyler Rinker suggested:
file_path_sans_ext(basename(filepath))
# [1] "ABCD"