Get filename without extension in R

后端 未结 7 1866
野趣味
野趣味 2020-12-01 05:56

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

7条回答
  •  攒了一身酷
    2020-12-01 06:46

    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"
    

提交回复
热议问题