我想沿着路径复制一个文件,然后通过 R 中的
file.copy()
将其移动到另一个位置。
说,文件在这里:
C:/Folder1/Folder2/Some_file_10.xlsx
我想把它移到这里:
C:/Folder1/AnotherFolder/Some_file_10.xlsx
文件名为
Some_file_10.xlsx
。文件名中的 10
是任意的,会在运行新代码时发生变化,但它始终是两位数字。我如何使用 file.copy()
、paste()
和某种类型的字符匹配包来检测 Some_file_10
并使用相同的名称移动它,如下所示:
file.copy(from = paste0("C:/Folder1/Folder2/Some_file_",
function(),
".xlsx"),
to = paste0("C:/Folder1/AnotherFolder/Some_file_",
function(),
".xlsx))
附加说明:此路径上不会有其他具有相同
Some_file_
起始字符的文件。
使用 Sys.glob 查找文件。对于
to=
参数,我们只需要指定目录。
file.copy(from = Sys.glob("C:/Folder1/Folder2/Some_file_*.xlsx"),
to = "C:/Folder1/AnotherFolder")