I have imported a few thousand xls files into a data.frame and I added a column with the filename.
Thus I have the data
data1 data2 data3 filname A
df$filname <- sub("^(\\d{3}).*$", "\\1", df$filname)
or
df$filname <- substr(df$filname, 0, 3)