简体   繁体   English

如何使用 R 重命名文件?

[英]How to rename files using R?

I have a bunch of files that have "_001" in their file names.我有一堆文件名中有“_001”的文件。 I followed a script posted online and I am at the directory where all my files are located:我按照在线发布的脚本进行操作,并且位于我所有文件所在的目录中:

filez <- list.files()
sapply(filez, function(X) {file.rename(from=x, to=sub(pattern="_001", replacement="", x))})

But I keep getting this error message:但我不断收到此错误消息:

Error in file.rename(from = x, to = sub(pattern = "001", replacement = "")): object 'x' not found". file.rename(from = x, to = sub(pattern = "001", replacement = "")) 错误:找不到对象 'x'”。

Can anyone help me solve this problem?谁能帮我解决这个问题?

I don't think you need to do this with sapply at all.我认为您根本不需要使用sapply来执行此操作。 (This will bypass the problem you are having where the arguments to file.rename and sub should have been X .) Try this instead: (这将绕过您遇到的问题,其中file.renamesub的参数应该是X 。)试试这个:

filez <- list.files()
file.rename(from=filez, to=sub(pattern="_001", replacement="", filez))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM