0

I am trying to extract the name and properties of files in a folder. I am using the code below, which works well, except that it does not extract the file name in a column. The first column has the full path to the file, but is there a way to get the file name with extension (eg file1.pdf) in a separate column?

data_files <- file.info(list.files(path = choose.folder(), 
    full.names = T, recursive = T, pattern = ".pdf$", ignore.case = TRUE))

Update: I was told about this question which is similar: Find file name from full file path

which is similar but I still prefer the answers below as they are more detailed and explained the steps in more details.

Bahi8482
  • 489
  • 5
  • 15

2 Answers2

2

try:

data_files$filename = basename(rownames(data_files))
akrun
  • 874,273
  • 37
  • 540
  • 662
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • thanks a lot, this works. If you know of any articles describing more r commands to work with file lists, please send the link to benefit from. thanks again. – Bahi8482 Sep 25 '20 at 22:15
2

We can use basename directly on the list.files

basename(list.files(path = choose.folder(), 
    full.names = T, recursive = T, pattern = ".pdf$", ignore.case = TRUE))
akrun
  • 874,273
  • 37
  • 540
  • 662