5

I have a folder with different files. What I'm trying to do is to extract file names without extensions of csv files only.

for example: if i have a folder with files

cp1.csv 
cp2.csv 
sd.exe 

I'd like to get a vector:

"cp1" "cp2" 
jon
  • 11,186
  • 19
  • 80
  • 132
Alex Kors
  • 182
  • 1
  • 4
  • 10

2 Answers2

7

You could use a list.files() gsub() combo

basenames<-gsub("\\.csv$","", list.files(pattern="\\.csv$"))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
5

An alternative to gsub would be file_path_sans_extension from "tools". Try:

library(tools)
file_path_sans_ext(list.files(pattern = "*.csv"))

Not much added here, but it's still a fun function to know about :-)

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485