Reputation: 464
I have a file name called 0.pdf
, and I want to move this file to a different folder in the same directory called issues
. But I also want to keep this 0.pdf
in the original place because this file is needed for other functions. I can do the below to move the file to the issues
folder.
file.copy("0.pdf","issues")
However, I want to also rename the "0.pdf" in the new folder issues
based on a variable value x
, if x=5
, I want the file name in the issues
folder to be 5.pdf
for example. How can I do that? If I use file.rename
then file.copy
, it would get rid of the 0.pdf
in the original place. How can I move the file, then rename the file in the new folder?
x=5
file.rename(paste(x,".pdf"))
file.copy(paste(x,".pdf"), "issues")
Upvotes: 0
Views: 74
Reputation: 545578
file.copy
takes either a directory path or the full file path as its to
argument. So you can write:
file.copy('0.pdf', file.path('issues', paste0(x, '.pdf')))
Upvotes: 1
Reputation: 388962
You can use -
x <- 5
filename <- "0.pdf"
file.copy(filename,paste0("issues/", sub('.*(?=\\.pdf)',x,filename, perl = TRUE)))
sub
would replace the filename (without extension) to the value x
.
sub('.*(?=\\.pdf)', x, filename, perl = TRUE)
#[1] "5.pdf"
Upvotes: 1