使用 R 没有明确模式的文件重命名(字符串替换)

发布于 2025-01-09 03:25:57 字数 459 浏览 3 评论 0原文

目前,我正在处理一长串文件。 它们的名称模式为 SB_xxx_(零件)。 (不同的扩展名),其中 xxx 指的是商品代码。

SB_19842.png
SB_19842_head.png
SB_19842_hand.png
SB_19842_head.pdf
...

发现其中很多代码都有错误的输入。 我手头有两列:一列是旧代码,一列是新代码(假设 A 和 B)。我希望将文件名中的所有旧代码更改为新代码。

  old    new
12154  24124
92482  02425
    .....

我的第一个想法是使用 file.rename() 然而,这是一种一对一的改变方法。我无法执行此操作,因为每个项目都有不同数量的部件和不同的文件扩展名。

是否有任何递归方法可以简单地将 A 中带有字符串的所有不正确的文件名更改为 B 中的字符串?有人有想法吗?

Currently, I am working with a long list of files.
They have a name pattern of SB_xxx_(parts). (different extensions), where xxx refers to an item code.

SB_19842.png
SB_19842_head.png
SB_19842_hand.png
SB_19842_head.pdf
...

It is found that many of these codes have incorrect entries.
I got two columns in hand: One is for old codes and one is new codes (let's say A & B). I hope to change all those old codes in the file names to the new code.

  old    new
12154  24124
92482  02425
    .....

My first thought is to use file.rename()
However, it is a one-to-one changing approach. I cannot do this because every item has a different number of parts and different file extensions.

Is there any recursive method that can simply change all incorrect file names with strings in A and replace them with strings in B? Anyone get an idea, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅一夜美梦 2025-01-16 03:25:57

末尾带有 purrr::map2 的循环解决方案:

library(purrr)
#create files to rename
file.create("SB_19842.png")
file.create("SB_19842_head.png")
file.create("SB_19842_hand.png")
file.create("SB_19842_head.pdf")
file.create("SB_12154.png")
file.create("SB_12154_head.png")
file.create("SB_12154_hand.png")
file.create("SB_12154_head.pdf")
# a dataframe with old a nd new patterns
file_names <- data.frame(
  old = c("19842", "12154"),
  new = c("new1", "new2")
)
# old filenames from the directory, specify path if needed
file_names_SB <- list.files(pattern = "SB_")
# function to substitute one type of code with another
sub_one_code <- function(old_code, new_code, file_names_original){
  gsub(paste0("SB_", old_code), paste0("SB_", new_code), file_names_original)
}
# loop to substitute all codes
new_file_names <- file_names_SB
for (row in 1:nrow(file_names)){
  new_file_names <- sub_one_code(file_names[row, "old"], file_names[row, "new"], new_file_names)
}
# rename all the files
map2(file_names_SB,
     new_file_names,
     file.rename)

@thelatemail 提供了一个链接,其中包含用于生成新文件名的更优雅的解决方案。

A loop solution with purrr::map2 at the end:

library(purrr)
#create files to rename
file.create("SB_19842.png")
file.create("SB_19842_head.png")
file.create("SB_19842_hand.png")
file.create("SB_19842_head.pdf")
file.create("SB_12154.png")
file.create("SB_12154_head.png")
file.create("SB_12154_hand.png")
file.create("SB_12154_head.pdf")
# a dataframe with old a nd new patterns
file_names <- data.frame(
  old = c("19842", "12154"),
  new = c("new1", "new2")
)
# old filenames from the directory, specify path if needed
file_names_SB <- list.files(pattern = "SB_")
# function to substitute one type of code with another
sub_one_code <- function(old_code, new_code, file_names_original){
  gsub(paste0("SB_", old_code), paste0("SB_", new_code), file_names_original)
}
# loop to substitute all codes
new_file_names <- file_names_SB
for (row in 1:nrow(file_names)){
  new_file_names <- sub_one_code(file_names[row, "old"], file_names[row, "new"], new_file_names)
}
# rename all the files
map2(file_names_SB,
     new_file_names,
     file.rename)

@thelatemail provided a link with more elegant solutions for generating new file names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文