R将整个文件夹移到另一个目录

时间:2018-10-03 21:46:46

标签: r directory move

我想将整个文件夹从一个目录移动到另一个目录,这是我的代码,

folder_old_path = "C:/Users/abc/Downloads/managerA"
path_new = "C:/User/abc/Desktop/managerA"
current_files = list.files(folder_old_path, full.names = TRUE)
file.copy(from = current_files, to = path_new, 
          overwrite = recursive, recursive = FALSE, copy.mode = TRUE)

但是,我收到此错误消息

  

file.copy中的错误(从= current_files,到= path_new,覆盖=递归,:     “来自”文件多于“至”文件

有什么解决办法吗?非常感谢您的帮助!

4 个答案:

答案 0 :(得分:0)

似乎不需要current_files = list.files(folder_old_path, full.names = TRUE)步骤。如果我对R file documentation的理解是正确的,那么您应该可以使用以下内容:

folder_old_path = "C:/Users/abc/Downloads/managerA"
path_new = "C:/User/abc/Desktop/managerA"
file.copy(from = folder_old_path, to = path_new, 
          overwrite = recursive, recursive = FALSE, copy.mode = TRUE)

如果这不起作用,则必须创建一个新文件列表(在current_files上进行迭代,并为列表中的每个项目将folder_old_path替换为folder_new_path ),然后致电file.copy

folder_old_path = "C:/Users/abc/Downloads/managerA"
path_new = "C:/User/abc/Desktop/managerA"
current_files = list.files(folder_old_path, full.names = TRUE)
new_files = # replace folder_old_path with path_new for every file in current_files
file.copy(from = current_files, to = new_files, 
          overwrite = recursive, recursive = FALSE, copy.mode = TRUE)

...(当然)全部假设folder_old_pathpath_new同时存在,并且您对其拥有正确的权限。

链接的页面确实包含有关Windows路径的警告/说明:

  

不能保证这些功能可以处理Windows   d:path形式的相对路径:请尝试使用d:./path。在   特别地,d:无法识别为目录。 \\?\前缀也不是   (和类似)。

答案 1 :(得分:0)

library(ff)
from <- "~/Path1/"            #Current path of your folder
to   <- "~/Path2/"            #Path you want to move it.
path1 <- paste0(from,"NameOfMyFolder")
path2 <- paste0(to,"NameOfMyFolder")
file.move(path1,path2)

尝试使用此小代码。

答案 2 :(得分:0)

在linux上,您应该能够简单地进行以下操作:

1)视需要制作OTHER_DIR。如果它是OUTPUT_DIR的子目录,则: dir.create(file.path(OUTPUT_DIR, OTHER_DIR), showWarnings = FALSE) setwd(file.path(OUTPUT_DIR, OTHER_DIR))

dir.create()仅在目录存在时显示警告。如果要查看警告,只需移除showWarnings = FALSE

如果它只是与OUTPUT_DIR处于同一级别的另一个目录,则:

dir.create(OTHER_DIR)

2)然后移动文件(例如,如果OTHER_DIR与OUTPUT_DIR处于同一级别):

file.rename("C:/OUTPUT_DIR/file.csv", "C:/OTHER_DIR/file.csv")

答案 3 :(得分:0)

最简单:

file.rename(folder_old_path, path_new)

如果要检查path_new是否已存在,可以将以上内容扩展为:

if (dir.exists(path_new) {
  print(paste("already exists so recurively deleting path_new", path_new))
  unlink(path_new, recursive = TRUE)
}