使用 r 库重命名文件(“tidyverse”)

问题描述 投票:0回答:1

我正在尝试使用库(“tidyverse”)重命名 r 中的一堆文件。

原样:Snap-551-图像导出-01_c1_ORG.tif

粗体部分是一个连续的数字,从01开始到150,每个数字由_c1_c2

组合而成

将是:001_551_c1_AE2315_01.tif

前 20 个文件来自同一样本。因此,这些的命名应具有相同的样本编号,此处为粗体的001。新文件名末尾应使用原始编号。

enter image description here

我非常感谢您对此的任何帮助:-) 非常感谢,安迪

r tidyverse
1个回答
0
投票

也许这个简单的例子适合您:

# text files in current working directory
A.txt, B.txt, C.txt

# get all files with the unique extension in your cwd
files <- list.files(pattern = "*.txt")

# rename the files by passing a vector of the new file names to the file.rename() function in the order of files in your cwd
file.rename(from = files,
            to = c("Apt.txt","Bart.txt","Dart.txt"))

# renamed files in cwd
Apt.txt, Bart.txt, Dart.txt

这是一个非常简单的示例,但我希望它能为您指明正确的方向。

© www.soinside.com 2019 - 2024. All rights reserved.