rocknRrr
rocknRrr

Reputation: 421

R subset file name between characters (Not Full Path)

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

Upvotes: 0

Views: 146

Answers (1)

Duck
Duck

Reputation: 39585

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"

Upvotes: 1

Related Questions