R - File. Copy Function
As Part of a Larger Script, I Need to Move All Files from One Directory to Another. I Wrote a Simple Script to Do It, and Was Working Fine, but for Some...
As part of a larger script, I need to move all <.csv> files from one directory to another. I wrote a simple script to do it, and was working fine, but for some reason, it is not working now, and I am going nuts trying to figure out what I'm doing wrong.
The code is:
rawPath <- "./test_dir1"
dataPath <- "./test_dir2"
dataFiles <- dir(rawPath, "*.csv", ignore.case = TRUE, all.files = TRUE)
file.copy(dataFiles, dataPath, overwrite = TRUE )
But I get the following error:
Warning messages:
1: In file.copy(dataFiles, dataPath, overwrite = TRUE) : problem copying .\test_dir1\11085.lis.csv to C:\Users\Desktop\test_dir2\11085.lis.csv: No such file or directory
One error message for each file
Please, find trial directories and files that are a simplified version of what I have in the following link:
Any help would be appreciated. Thanks!
1 Answer
Your problem is that you have extracted the file names relative to rawPath, and then are trying to use that in file.copy while you're in a different directory. Running your code, look at dataFiles:
dataFiles
# [1] "11085.lis.csv" "13087.lis.csv" "17089.lis.csv" "5081.lis.csv" "7083.lis.csv"
You want
file.copy(paste(rawPath, dataFiles, sep = .Platform$file.sep), dataPath, overwrite = TRUE)
# [1] TRUE TRUE TRUE TRUE TRUE
Or alternatively:
file.copy(file.path(rawPath, dataFiles), dataPath, overwrite = TRUE)
# [1] TRUE TRUE TRUE TRUE TRUE
To reproduce:
dir.create("test_dir1")
dir.create("test_dir2")
files <- paste0(c(5081, 7083, 11085, 13087, 17089), ".lis.csv")
file.create(paste("test_dir1", files, sep = .Platform$file.sep))
# [1] TRUE TRUE TRUE TRUE TRUE
dir("test_dir1")
# [1] "11085.lis.csv" "13087.lis.csv" "17089.lis.csv" "5081.lis.csv" "7083.lis.csv"
dir("test_dir2")
# character(0)
rawPath <- "./test_dir1"
dataPath <- "./test_dir2"
dataFiles <- dir(rawPath, "*.csv", ignore.case = TRUE, all.files = TRUE)
# To reproduce the error:
file.copy(dataFiles, dataPath, overwrite = TRUE )
# To run without error:
file.copy(file.path(rawPath, dataFiles), dataPath, overwrite = TRUE)