简体   繁体   English

如何在R中重命名多文件

[英]How to rename mutliples files in r

I need to convert over 100 images name into a format like: SITE_T001_L001.jpg, where Site is CGS1, T= TUBES, L= image number. 我需要将100个以上的图像名称转换为类似SITE_T001_L001.jpg的格式,其中Site为CGS1,T = TUBES,L =图像编号。

All those images are contain into a single file named CGS1 (the site), subdivided by file named accordingly to their tubes number. 所有这些图像都包含在一个名为CGS1(站点)的文件中,并按相应的管号对文件进行细分。 Then the images are organised by date. 然后按日期组织图像。 This order represents the image number. 该顺序代表图像编号。 The first one is 1, the second one is two.(the alpahabetic order is not correct) 第一个为1,第二个为2(字母顺序不正确)

here, I have a graphical representation: 在这里,我有一个图形表示: 在此处输入图片说明 I found how to do it manually in R 我发现如何在R中手动进行

file.rename("Snap_029.jpg",
        paste("CGS1","T001","L003", ".jpg", sep = "_"))

but is there anyway to automate it with a loop? 但是总有没有用循环来自动化它呢?

In more details - as requested in the response: I have this series of input filenames (including leading path)- ordered by dates of modification (important). 更详细地-根据响应中的要求:我有一系列输入文件名(包括前导路径)-按修改日期排序(重要)。

file_list
[1] "CGS1/1/Snap_001.jpg" "CGS1/1/Snap_002.jpg" "CGS1/1/Snap_005.jpg" "CGS1/2/Snap_006.jpg" "CGS1/2/Snap_007.jpg" "CGS1/2/Snap_082.jpg"

I am looking to modify the name of each images following the main folder CGS1, the subfolder from T001 to T002, and following the date of modification from L001 to L003 as this output filenames 我正在寻找在主文件夹CGS1,从T001到T002的子文件夹以及从L001到L003的修改日期之后的每个图像的名称,作为此输出文件名

new_file_list
 [1] "CGS1_T001_L001.jpg" "CGS1_T001_L002.jpg" "CGS1_T001_L003.jpg" "CGS1_T002_L001.jpg" "CGS1_T002_L002.jpg" "CGS1_T002_L003.jpg"

Try this: 尝试这个:

file_list <- list.files(path = "...", recursive = TRUE, pattern = "\\.jpg$")
### for testing
file_list <- c(
  "CGS1/1/Snap_001.jpg", "CGS1/1/Snap_005.jpg", "CGS1/1/Snap_002.jpg",
  "CGS1/2/Snap_006.jpg", "CGS1/2/Snap_007.jpg", "CGS1/2/Snap_0082.jpg"
)

spl <- strsplit(file_list, "[/\\\\]")
# ensure that all files are exactly two levels down
stopifnot(all(lengths(spl) == 3))

m <- do.call(rbind, spl)
m
#      [,1]   [,2] [,3]           
# [1,] "CGS1" "1"  "Snap_001.jpg" 
# [2,] "CGS1" "1"  "Snap_005.jpg" 
# [3,] "CGS1" "1"  "Snap_002.jpg" 
# [4,] "CGS1" "2"  "Snap_006.jpg" 
# [5,] "CGS1" "2"  "Snap_007.jpg" 
# [6,] "CGS1" "2"  "Snap_0082.jpg"

From this, we'll update the second/third columns to be what you expect. 由此,我们将第二/第三列更新为您期望的值。

# either one (not both), depending on if you are guaranteed integers
m[,2] <- sprintf("T%03.0f", as.integer(m[,2]))
# ... or if you may have non-numbers
m[,2] <- paste0("T", strrep("0", max(0, 3 - nchar(m[,2]))), m[,2])

# since we really don't care about 'Snap_001.jpg' (etc), we can discard the third column
new_file_list <- apply(m[,1:2], 1, paste, collapse = "_")
# back-street way of applying sequences to each CGS/T combination while preserving order
for (prefix in unique(new_file_list)) {
  new_file_list[new_file_list == prefix] <- sprintf("%s_L%03d.jpg",
                                                    new_file_list[new_file_list == prefix],
                                                    seq_len(sum(new_file_list == prefix)))
}
new_file_list
# [1] "CGS1_T001_L001.jpg" "CGS1_T001_L002.jpg" "CGS1_T001_L003.jpg"
# [4] "CGS1_T002_L001.jpg" "CGS1_T002_L002.jpg" "CGS1_T002_L003.jpg"

Now it's a matter of renaming. 现在是重命名的问题。 Note that this will move all files into the current directory. 请注意,这会将所有文件移动到当前目录。

file.rename(file_list, new_file_list)

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

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