我有两个文件夹(称为“A”和“B”),它们在一个文件夹中(称为“输入”)。我想将“A”和“B”复制到另一个文件夹中(称为“输出”)。我能用R来做到这一点吗?
将当前目录的文件复制到它们的新目录中
currentfiles
是要复制的文件列表,newlocation
是要复制到的目录。
如果您没有列出当前的文件,则需要循环遍历您的工作目录。
file.copy(from=currentfiles, to=newlocation,
overwrite = TRUE, recursive = FALSE,
copy.mode = TRUE)
这是用于删除您的旧文件
file.remove(currentfiles)
overwrite
是逻辑型的。请将其更改为TRUE
或FALSE
。 - pat-scurrent_folder <- "C:/Users/Bhabani2077/Desktop/Current"
new_folder <- "C:/Users/Bhabani2077/Desktop/Ins"
list_of_files <- list.files(current_folder, ".py$")
# ".py$" is the type of file you want to copy. Remove if copying all types of files.
file.copy(file.path(current_folder,list_of_files), new_folder)
list_of_files
时,您当前文件夹的名称错误。 - vladlifile.copy()
不起作用的原因,基于我对“文件操作”R文档的理解 - 输入到file.copy()
的参数必须是文件(而不是目录)的“from”字段,但可以是“to”字段的文件或目录。fs包提供了一个替代方案,可以精确地回答原始问题。
library(fs)
fs::dir_tree()
#> .
#> +-- copy_folder.R
#> +-- copy_folder.Rproj
#> \-- input
#> +-- A
#> | +-- C
#> | \-- exampleA.txt
#> +-- B
#> | \-- exampleB.txt
#> \-- D
fs::dir_copy("input/A", "output/A")
fs::dir_copy("input/B", "output/B")
fs::dir_tree()
#> .
#> +-- copy_folder.R
#> +-- copy_folder.Rproj
#> +-- input
#> | +-- A
#> | | +-- C
#> | | \-- exampleA.txt
#> | +-- B
#> | | \-- exampleB.txt
#> | \-- D
#> \-- output
#> +-- A
#> | +-- C
#> | \-- exampleA.txt
#> \-- B
#> \-- exampleB.txt
fs::dir_copy("input", "output")
2021年07月03日由reprex包(v2.0.0)创建
?file.copy
。 - Mamoun Benghezal