Get filename without extension in R

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

    Here is an implementation that works for compression and multiple files:

    remove.file_ext <- function(path, basename = FALSE) {
      out <- c()
      for (p in path) {
        fext <- file_ext(path)
        compressions <- c("gzip", "gz", "bgz", "zip")
        areCompressed <- fext %in% compressions
        if (areCompressed) {
          ext <- file_ext(file_path_sans_ext(path, compression = FALSE))
          regex <- paste0("*\\.",ext,"\\.", fext,"$")
        } else {
          regex <- paste0("*\\.",fext,"$")
        }
        new <- gsub(pattern = regex, "", path)
        out <- c(out, new)
      }
      return(ifelse(basename, basename(out), out))
    }
    
    0 讨论(0)
提交回复
热议问题