使用R根据文件名重命名目录中的文件
Renaming files in directory based on filename using R
我有一个数据框 (FilesDf),其中包含每个文件名和我需要对其进行的替换 (FilesDf$FileTags)。
FileName Filename FileTags
H:/name/+Sm,Jon.docx +Sm,Jon.docx RR UB AF-
H:/name/+Suth,Jane.docx +Suth,Jane.docx AF-
H:/name/+Dunn,Robert.docx +Dunn,Robert.docx RR LL-
对于此文件夹中的每个文件名,我都需要附加 FileTags 作为前缀。文件名需要如下所示:
RR UB AF-Sm,Jon.docx
AF-Suth,Jane.docx
RR LL-Dunn,Robert.docx
我的尝试:
Filepath <- "H:/name/"
files <- list.files(Filepath,pattern = "*.doc",full.names = T)
nrow<-nrow(FilesDf)
for(i in nrow){
sapply(files,FUN=function(eachPath){
file.rename(from=eachPath,to= sub(pattern="\+",
FilesDf$FileTags[i],eachPath))
})
}
但这会导致所有文件都具有相同的前缀,而不是具有与文件名正确对应的前缀。
我建议您分阶段进行操作,部分是为了确保它正常工作(测试),部分是因为它很容易 maintain/extend。
FilesDf$FileName2 <- file.path(dirname(FilesDf$FileName),
gsub("\+", "", paste0(FilesDf$FileTags, FilesDf$Filename)))
FilesDf
# FileName Filename FileTags FileName2
# 1 H:/name/+Sm,Jon.docx +Sm,Jon.docx RR UB AF- H:/name/RR UB AF-Sm,Jon.docx
# 2 H:/name/+Suth,Jane.docx +Suth,Jane.docx AF- H:/name/AF-Suth,Jane.docx
# 3 H:/name/+Dunn,Robert.docx +Dunn,Robert.docx RR LL- H:/name/RR LL-Dunn,Robert.docx
如果新名称 ($FileName2
) 看起来不错,那么
ign <- mapply(file.rename, FilesDf$FileName, FilesDf$FileName2)
应该可以。
(我最初被 $FileName
vs $Filename
分散了注意力,而错过了第二个 ...)
数据:
FilesDf <- structure(list(FileName = c("H:/name/+Sm,Jon.docx", "H:/name/+Suth,Jane.docx",
"H:/name/+Dunn,Robert.docx"), Filename = c("+Sm,Jon.docx", "+Suth,Jane.docx",
"+Dunn,Robert.docx"), FileTags = c("RR UB AF-", "AF-", "RR LL-"
)), row.names = c(NA, -3L), class = c("data.frame"))
我有一个数据框 (FilesDf),其中包含每个文件名和我需要对其进行的替换 (FilesDf$FileTags)。
FileName Filename FileTags
H:/name/+Sm,Jon.docx +Sm,Jon.docx RR UB AF-
H:/name/+Suth,Jane.docx +Suth,Jane.docx AF-
H:/name/+Dunn,Robert.docx +Dunn,Robert.docx RR LL-
对于此文件夹中的每个文件名,我都需要附加 FileTags 作为前缀。文件名需要如下所示:
RR UB AF-Sm,Jon.docx
AF-Suth,Jane.docx
RR LL-Dunn,Robert.docx
我的尝试:
Filepath <- "H:/name/"
files <- list.files(Filepath,pattern = "*.doc",full.names = T)
nrow<-nrow(FilesDf)
for(i in nrow){
sapply(files,FUN=function(eachPath){
file.rename(from=eachPath,to= sub(pattern="\+",
FilesDf$FileTags[i],eachPath))
})
}
但这会导致所有文件都具有相同的前缀,而不是具有与文件名正确对应的前缀。
我建议您分阶段进行操作,部分是为了确保它正常工作(测试),部分是因为它很容易 maintain/extend。
FilesDf$FileName2 <- file.path(dirname(FilesDf$FileName),
gsub("\+", "", paste0(FilesDf$FileTags, FilesDf$Filename)))
FilesDf
# FileName Filename FileTags FileName2
# 1 H:/name/+Sm,Jon.docx +Sm,Jon.docx RR UB AF- H:/name/RR UB AF-Sm,Jon.docx
# 2 H:/name/+Suth,Jane.docx +Suth,Jane.docx AF- H:/name/AF-Suth,Jane.docx
# 3 H:/name/+Dunn,Robert.docx +Dunn,Robert.docx RR LL- H:/name/RR LL-Dunn,Robert.docx
如果新名称 ($FileName2
) 看起来不错,那么
ign <- mapply(file.rename, FilesDf$FileName, FilesDf$FileName2)
应该可以。
(我最初被 $FileName
vs $Filename
分散了注意力,而错过了第二个 ...)
数据:
FilesDf <- structure(list(FileName = c("H:/name/+Sm,Jon.docx", "H:/name/+Suth,Jane.docx",
"H:/name/+Dunn,Robert.docx"), Filename = c("+Sm,Jon.docx", "+Suth,Jane.docx",
"+Dunn,Robert.docx"), FileTags = c("RR UB AF-", "AF-", "RR LL-"
)), row.names = c(NA, -3L), class = c("data.frame"))