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

问题描述 投票:0回答:1

我试图在同一目录中制作文件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"))

r
1个回答
0
投票

你可以试试这个:

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))
© www.soinside.com 2019 - 2024. All rights reserved.