使用 table 重命名存储在文件夹中的文件
Rename files stored in a folder using a table
我有一个文件夹,其中包含来自各种来源的大量文件,主要是照片和视频。我在 Excel 中还有一个 table,其中有两列:一列是这些文件存储在文件夹中的原始名称,另一列是要重命名的新名称。
我的问题是:如何使用 Excel 中的 table 作为参考列表重命名存储在该文件夹中的文件?
library(tidyverse)
# The original files of the photos are stored in a folder
current.folder <- "C:/Users/Tests/Desktop/Field_photos_2019"
# Produce a table with mock-up names for the photos
raw.table <- tibble(IMG = rep(x = "IMG_", times = 10),
date.photo = rep(x = c("20190831", "20190901"), each = 5),
suffix = paste0(rep(x = "_", times = 10), sample(x = 1:100, size = 10), rep(x = ".jpg", times = 10)),
photo.title = paste0(IMG, date.photo, suffix))
# Tidy up the table to make it look as the real table in Excel
# Not sure if the mock-up names also need the file extension?
mock.table <- raw.table %>%
group_by(date.photo) %>%
mutate(no = 1:n()) %>%
ungroup() %>%
mutate(new.name = paste(date.photo, no, sep = "_")) %>%
select(photo.title, new.name)
R 有一个 file.rename
函数似乎可以满足您的需求
setwd(current.folder)
lapply(list.files(current.folder),
function(x){file.rename(from = x, to = mock.table[mock.table$photo.title == x,]$new.name)})
我有一个文件夹,其中包含来自各种来源的大量文件,主要是照片和视频。我在 Excel 中还有一个 table,其中有两列:一列是这些文件存储在文件夹中的原始名称,另一列是要重命名的新名称。
我的问题是:如何使用 Excel 中的 table 作为参考列表重命名存储在该文件夹中的文件?
library(tidyverse)
# The original files of the photos are stored in a folder
current.folder <- "C:/Users/Tests/Desktop/Field_photos_2019"
# Produce a table with mock-up names for the photos
raw.table <- tibble(IMG = rep(x = "IMG_", times = 10),
date.photo = rep(x = c("20190831", "20190901"), each = 5),
suffix = paste0(rep(x = "_", times = 10), sample(x = 1:100, size = 10), rep(x = ".jpg", times = 10)),
photo.title = paste0(IMG, date.photo, suffix))
# Tidy up the table to make it look as the real table in Excel
# Not sure if the mock-up names also need the file extension?
mock.table <- raw.table %>%
group_by(date.photo) %>%
mutate(no = 1:n()) %>%
ungroup() %>%
mutate(new.name = paste(date.photo, no, sep = "_")) %>%
select(photo.title, new.name)
R 有一个 file.rename
函数似乎可以满足您的需求
setwd(current.folder)
lapply(list.files(current.folder),
function(x){file.rename(from = x, to = mock.table[mock.table$photo.title == x,]$new.name)})