Get filename without extension in R

后端 未结 7 1826
野趣味
野趣味 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:25

    There's a built in file_path_sans_ext from the standard install tools package that grabs the file without the extension.

    tools::file_path_sans_ext("ABCD.csv")
    ## [1] "ABCD"
    
    0 讨论(0)
  • 2020-12-01 06:27

    You can try this also:

    data <- "ABCD.csv"
    gsub(pattern = "\\.csv$", "", data)
    
    #[1] "ABCD"
    

    This will be helpful in case of list of files as well, say

    data <- list.files(pattern="\\.csv$") , using the code will remove extension of all the files in the list.

    0 讨论(0)
  • 2020-12-01 06:27

    Loading the library needed :

    > library(stringr)
    

    Extracting all the matches from the regex:

    > str_match("ABCD.csv", "(.*)\\..*$")
         [,1]       [,2]  
    [1,] "ABCD.csv" "ABCD"
    

    Returning only the second part of the result, which corresponds to the group matching the file name:

    > str_match("ABCD.csv", "(.*)\\..*$")[,2]
    [1] "ABCD"
    

    EDIT for @U-10-Forward:

    It is basically the same principle as the other answer. Just that I found this solution more robust.

    Regex wise it means:

    • () = group

    • .* = any single character except the newline character any number of time

    • // is escape notation, thus //. means literally "."

    • .* = any characters any number of time again

    • $ means should be at the end of the input string

    The logic is then that it will return the group preceding a "." followed by a group of characters at the end of the string (which equals the file extension in this case).

    0 讨论(0)
  • 2020-12-01 06:34

    If you have filenames with multiple (possible extensions) and you want to strip off only the last extension, you can try the following.

    Consider the filename foo.bar.baz.txt this

    sub('\\..[^\\.]*$', '', "foo.bar.baz.txt")
    

    will leave you with foo.bar.baz.

    0 讨论(0)
  • 2020-12-01 06:37

    You can use sub or substr

    sub('\\.csv$', '', str1) 
    #[1] "ABCD"
    

    or

    substr(str1, 1, nchar(str1)-4)
    #[1] "ABCD"
    

    Using the 'file_path' from @JasonV's post

    sub('\\..*$', '', basename(filepath))
    #[1] "ABCD"
    

    Or

    library(stringr)
    str_extract(filepath,  perl('(?<=[/])([^/]+)(?=\\.[^.]+)'))
    #[1] "ABCD"
    

    data

    str1 <- 'ABCD.csv'
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
提交回复
热议问题