首页 > 解决方案 > 更改某些文件名中的子字符串

问题描述

如何更改文件夹中所有文件名称中的子字符串?

示例,文件夹中的文件列表(输入):

c("Comment1 03_2020.docx", "Comment2 03_2020.docx", "Comment4 03_2020.docx")

要更改的子字符串:从“03_2020”到“04_2023”

期望的输出:

c("Comment1 04_2023.docx", "Comment2 04_2023.docx", "Comment4 04_2023.docx")

谢谢

标签: r

解决方案


你正在寻找gsub(). 以下将实现您正在寻找的内容:

# list the files in a directory (if you want the absolute path names, use full.names = T
files <- list.files("path/to/dir")

# Replace string with gsub
files_new <- gsub("03_2020", "04_2023", files)

# Then you can rename with rename() or whatever you had in mind

推荐阅读