0

I would like to subset file names only from folders. So from name like below,

"C:/Users/UserName/Document/Folder1/Foder2/Data_2020_12_15_Test Set.docx"
"C:/Users/UserName/Document/Folder1/Foder2/Doc_2020_08_12_Test Set.docx"

How to subset strings after backslash to docx, and apply it to a list in order to get output as below?

 Data_2020_12_15_Test Set.docx
 Doc_2020_08_12_Test Set.docx
rocknRrr
  • 341
  • 1
  • 10

1 Answers1

1

You can try this over the vector with the strings containing the filenames:

#Data
val <- c('C:/Users/UserName/Document/Folder1/Foder2/Data_2020_12_15_Test Set.docx')
#Code
gsub('^(?:[^/]*/)*\\s*(.*)', '\\1', val)

Output:

[1] "Data_2020_12_15_Test Set.docx"
Duck
  • 39,058
  • 13
  • 42
  • 84