Detect a file with two arbitrary characters along a file path and move it to another file path in R

huangapple go评论117阅读模式
英文:

Detect a file with two arbitrary characters along a file path and move it to another file path in R

问题

我想要使用 file.copy() 在R中沿路径复制文件并将其移动到另一个位置。

例如,文件位于这里:
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_ 开头的文件。

英文:

I am looking to copy a file along a path and move it to another location via file.copy() in R.

Say, the file exists here:

C:/Folder1/Folder2/Some_file_10.xlsx

And I want to move it here:

C:/Folder1/AnotherFolder/Some_file_10.xlsx

The file name is Some_file_10.xlsx. The 10 in the file name is arbitrary and will change when new code is run, but it will always be two numeric digits. How can I use file.copy(), paste(), and something type of character matching package to both detect Some_file_10 and move it with the same name, like so:

file.copy(from = paste0("C:/Folder1/Folder2/Some_file_",
                        function(), 
                        ".xlsx"),
          to = paste0("C:/Folder1/AnotherFolder/Some_file_",
                       function(),
                       ".xlsx))

An additional note: There will be no other files with the same starting characters of Some_file_ along this path.

答案1

得分: 2

Use Sys.glob to find the file. For the to= argument we only need to specify the directory.

file.copy(from = Sys.glob("C:/Folder1/Folder2/Some_file_*.xlsx"),
          to = "C:/Folder1/AnotherFolder")
英文:

Use Sys.glob to find the file. For the to= argument we only need to specify the directory.

file.copy(from = Sys.glob("C:/Folder1/Folder2/Some_file_*.xlsx"),
          to = "C:/Folder1/AnotherFolder")

huangapple
  • 本文由 发表于 2023年3月9日 23:46:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定