r - 更改某些文件名中的子字符串
问题描述
如何更改文件夹中所有文件名称中的子字符串?
示例,文件夹中的文件列表(输入):
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")
谢谢
解决方案
你正在寻找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
推荐阅读
- api - 我不能使用 RTCVideoRenderer 的镜像属性。如何解决这个问题?
- c - 如何优化这个级数算法?
- python - visual studio 2013 - python ImportError:DLL加载失败
- c# - 我怎样才能使这个 DateTime.Now.ToFileTimeUtc() 成为线程安全的?
- json - 使用 JSONPath 从 JSON 对象中获取值
- javascript - 'appendChild' of null 即使在定义时
- elasticsearch - 弹性动态映射 - 仅限于对象
- python - 按回车躲避...(快速时间事件)[Python 3]
- c++ - 绑定成员函数指针
- r - 使用 R 读取包含科学数字的 csv 文件