Q:

Apply common header names to all files in the folder

I have 4 files in the folder named import_xxx.xlsx.
I need to apply below tasks,
1.First apply common header names to all files in the folder.

Write the applied corrections as seperate file in another folder.

I have tried below code.
1.Read the list of files
filenames_list <- list.files(pattern= ".xls", full.names=TRUE) 

My question is how to search for header names and apply naming the changed name to all files. 
My column names is as follows with sample data.
  Sr No     Invoice Date    Invoice No    Payer Name    IGMNo    Container No    Size    Type    Act. gate in Date    Container Agent    Container  Agent  Name    Importer Name    CHA Code    CHA Name    Activity Description    Amount    Service Tax    Total                                                                  
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  1    8-1-2018 12:12:29 AM    MII180800001    SAME DEUTZ FAHR INDIA PRIVATE LIMITED    2200750    ECMU9674562    40    GB    7-26-2018 4:50:35 AM    CLC007    CMA CGM    SAME DEUTZ FAHR INDIA PRIVATE LIMITED    CHS020    SEAKING CARGO SERVICES (I)  PVT LTD    Handling &  PNR Movement Charges-FCL    10400    1872    12272 
  2    8-1-2018 12:12:29 AM    MII180800001    SAME DEUTZ FAHR INDIA PRIVATE LIMITED    2200750    ECMU9674562    40    GB    7-26-2018 4:50:35 AM    CLC007    CMA CGM    SAME DEUTZ FAHR INDIA PRIVATE LIMITED    CHS020    SEAKING CARGO SERVICES (I)  PVT LTD    Value Added Charges    2000    360    2360                     

I need to perform below data transformation tasks, which converts lower case characters to upper case characters. i.e column of activity desciption.
data.frame(lapply(df$Activity Description, function(v) {
if (is.character(v)) return(toupper(v))
else return(v)
}))                
Do I need to loop through the files to write it? Following code will write the file but I need to loop through and write the applied changes for all files.
write.xlsx2(filename,"path") 

Can anyone help me on this to loop through files and perform header transformation and write the files within the loop?
Thanks.

A:

I would do something along the lines of the following. You will have to define the vector with the new common header, common_header.
(Untested.)
library(xlsx)

filenames_list <- list.files(pattern= "\\.xls", full.names=TRUE)

target_dir <- "path/to/target/directory/"

lapply(filenames_list, function(fl){
  DF <- read.xlsx2(fl)
  names(DF) <- common_header
  target_fl <- paste0(target_dir, basename(fl))
  write.xlsx2(DF, target_fl)
})

