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: https://www.dropbox.com/sh/7eqvyugc472w19i/AACIjlytbB4s55X0Ga1hmKQka?dl=0
Any help would be appreciated. Thanks!
copy() function as shown in the following R syntax. Within the file. copy function, we have to specify the directory path and file names of the first folder from which we want to copy the files, as well as the directory path and file names of the second folder to which we want to copy the files.
To move a file in R, use the move_files() function. The move_files() function is not a built-in R function. It is under the filesstrings library. To use the move_files() function, you need first to install the filesstrings library.
An R file is a script written in R, a programming language used for statistical analysis and graphing purposes. It contains code that can be executed within the R software environment. R files may include commands that create objects (functions, values, etc.) and produce visualizations of the computed data.
To check if the file or folder exists in R, use the file. exists() method. The file. exists() method returns the logical vector indicating whether the files named by its argument exist.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With