r - how to replace names of files from another folder? -
i have 4 files in folder dir1 , other 4 files in dir2
we can list files in both folders as:
dir1<- list.files("/data/myfiles1", "*.img", full.names = true) dir1 file_data_20000125.img file_data_20000126.img file_data_20000127.img file_data_20000128.img dir2<- list.files("/data/myfiles2", "*.img", full.names = true) newfile_01.img newfile_02.img newfile_03.img newfile_04.img now need replace the names of files in dir2 names of files in dir1. instance, newfile_01.img becomes file_data_20000125.img, newfile_02.img becomes file_data_20000126.img, , on.
i think should there may more faster way
basename used base name full fie path , using gsubto replace onld filename new filename
file.rename used replace file name
my code:
dir1<- list.files("/data/myfiles1", "*.img", full.names = true) dir2<- list.files("/data/myfiles2", "*.img", full.names = true) (a in 1:length(dir1)){ file.rename(from=dir2[a],to=gsub(basename(dir2[a]),basename(dir1[a]),dir2[a])) }
Comments
Post a Comment