file.copy 在同一个目录中不起作用



我正在尝试在同一目录中复制文件 ABC 并重命名


dirPath ="P:\test\"
fileABC =paste("file",  format(Sys.Date(), "%Y"), format(Sys.Date(), "%m"), sep="")
previousDate =as.Date(format(Sys.Date(), '%Y-%m-01')) - 1
previousDate2 =as.Date(format(previousDate, '%Y-%m-01')) - 1
fileXYZ =paste("file",  format(previousDate2, "%Y"), format(previousDate2, "%m"),".csv", sep="")
setwd(dirPath)
file.copy(file.path(dirPath, fileABC), dirPath, overwrite = FALSE)
file.rename(list.files(pattern=" - Copy.csv"), paste0(fileXYZ,".csv"))

你可以试试这个:

todir<-tempdir()
file.copy(file.path(dirPath, fileABC), tempdir(), overwrite = FALSE)
file.rename(file.path(todir, fileABC),paste0(file.path(todir, fileABC),"_copy"))
file.copy(paste0(file.path(todir, fileABC),"_copy"), dirPath, overwrite = FALSE)
file.rename(paste0(file.path(dirPath, fileABC),"_copy"), file.path(dirPath, fileXYZ))

相关内容

最新更新