使用 R 没有明确模式的文件重命名(字符串替换)
目前,我正在处理一长串文件。 它们的名称模式为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
末尾带有
purrr::map2
的循环解决方案:@thelatemail 提供了一个链接,其中包含用于生成新文件名的更优雅的解决方案。
A loop solution with
purrr::map2
at the end:@thelatemail provided a link with more elegant solutions for generating new file names.