如何使用R重命名文件? [英] How do I rename files using R?

查看:431
本文介绍了如何使用R重命名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为的文件夹中拥有700多个文件: 从1号到9号的文件在第一个月被命名:

I have over 700 files in one folder named as: files from number 1 to number9 are named for the first month:

water_200101_01.img  
water_200101_09.img  

从10号到30号的文件被命名为:

files from number 10 to number30 are named:

water_200101_10.img
water_200101_30.img

第二个月,依此类推: 从1到9的文件被命名为:

And so on for the second month: files from number 1 to number9 are named:

water_200102_01.img  
water_200102_09.img  

从10号到30号的文件被命名为:

files from number 10 to number30 are named:

water_200102_10.img
water_200102_30.img 

如何在不对文件进行任何更改的情况下重命名它们.只需更改名称,例如

How can I rename them without making any changes to the files. just change the nams, for example

water_1
water_2
...till...
water_700

推荐答案

file.rename将重命名文件,并且可以同时使用fromto名称的向量.

file.rename will rename files, and it can take a vector of both from and to names.

所以像这样:

file.rename(list.files(pattern="water_*.img"), paste0("water_", 1:700))

可能会工作.

如果要特别注意顺序,则可以对当前存在的文件列表进行排序,或者按照特定的模式进行排序,只需直接创建文件名向量即可(尽管我注意到700不是30的倍数)

If care about the order specifically, you could either sort the list of files that currently exist, or if they follow a particular pattern, just create the vector of filenames directly (although I note that 700 is not a multiple of 30).

我将搁置一个问题,你为什么要?"因为您似乎要丢弃文件名中的信息,但是大概该信息也包含在其他位置.

I will set aside the question, "why would you want to?" since you seem to be throwing away information in the filename, but presumably that information is contained elsewhere as well.

这篇关于如何使用R重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆