1

I have a folder that contains 2000 file of xls and xlsx extension whose name is somewhat related to the excel file city name (EX: DELHI123.xlsx, Delhi65.xls, DELhi456.xls, Mumbai989.xlsx, Indore123,Indore567 etc.)

I have another excel file having only one column that contains names of 900 files of the folder.

I want to keep only that 900 files whose name in present in the excel sheet and want to delete the remaing,

Subhashree
  • 108
  • 1
  • 1
  • 8

1 Answers1

0

You can use the file.remove-function for that.

An example implementation:

allfiles <- list.files(pattern = '.xls')
to_remove <- allfiles[!(allfiles %in% my900names)]
file.remove(to_remove)
Jaap
  • 81,064
  • 34
  • 182
  • 193