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
  • 10,926
  • 19
  • 76
  • 130
Alex Kors
  • 182
  • 4
  • 10

2 Answers2

7

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

basenames<-gsub("\\.csv$","", list.files(pattern="\\.csv$"))
MrFlick
  • 178,638
  • 15
  • 253
  • 268
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
  • 184,536
  • 28
  • 389
  • 466