如何使用 R 重命名文件?

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

water_200101_01.img
water_200101_09.img

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

water_200101_10.img
water_200101_30.img

以此类推,第二个月: 从1号到9号的文件名为:

water_200102_01.img
water_200102_09.img

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

water_200102_10.img
water_200102_30.img

如何重命名它们而不对文件进行任何更改。例如,只是更改名称

water_1
water_2
...till...
water_700
103513 次浏览

file.rename将重命名文件,它可以采用 fromto名称的向量。

比如:

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

可能有用。

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

我先不问“你为什么要这么做?”因为您似乎抛弃了文件名中的信息,但推测这些信息也包含在其他地方。

这是我为自己写的。它速度很快,允许在查找和替换中使用正则表达式,可以忽略文件后缀,并且可以显示在“试运行”中会发生什么,以及防止覆盖现有文件。

如果你在 Mac 上,它可以使用 apple 脚本来选择 Finder 中的当前文件夹作为目标文件夹。

umx_rename_file <- function(findStr = "Finder", replaceStr = NA, baseFolder = "Finder", test = TRUE, ignoreSuffix = TRUE, listPattern = NULL, overwrite = FALSE) {
umx_check(!is.na(replaceStr), "stop", "Please set a replaceStr to the replacement string you desire.")


# ==============================
# = 1. Set folder to search in =
# ==============================
if(baseFolder == "Finder"){
baseFolder = system(intern = TRUE, "osascript -e 'tell application \"Finder\" to get the POSIX path of (target of front window as alias)'")
message("Using front-most Finder window:", baseFolder)
} else if(baseFolder == "") {
baseFolder = paste(dirname(file.choose(new = FALSE)), "/", sep = "") ## choose a directory
message("Using selected folder:", baseFolder)
}


# =================================================
# = 2. Find files matching listPattern or findStr =
# =================================================
a = list.files(baseFolder, pattern = listPattern)
message("found ", length(a), " possible files")


changed = 0
for (fn in a) {
if(grepl(pattern = findStr, fn, perl= TRUE)){
if(ignoreSuffix){
# pull suffix and baseName (without suffix)
baseName = sub(pattern = "(.*)(\\..*)$", x = fn, replacement = "\\1")
suffix   = sub(pattern = "(.*)(\\..*)$", x = fn, replacement = "\\2")
fnew = gsub(findStr, replacement = replaceStr, x = baseName, perl= TRUE) # replace all instances
fnew = paste0(fnew, suffix)
} else {
fnew = gsub(findStr, replacement = replaceStr, x = fn, perl= TRUE) # replace all instances
}
if(test){
message(fn, " would be changed to:  ", omxQuotes(fnew))
} else {
if((!overwrite) & file.exists(paste(baseFolder, fnew, sep = ""))){
message("renaming ", fn, "to", fnew, "failed as already exists. To overwrite set T")
} else {
file.rename(paste0(baseFolder, fn), paste0(baseFolder, fnew))
changed = changed + 1;
}
}
}else{
if(test){
# message(paste("bad file",fn))
}
}
}
if(test & changed==0){
message("set test = FALSE to actually change files.")
} else {
umx_msg(changed)
}
}

如果希望将文件名中与给定模式匹配的某个部分替换为另一个模式。这对于同时重命名多个文件很有用。例如,这段代码将获取包含 foo 的所有文件,并在文件名中将 foo 替换为 bob。

file.rename(list.files(pattern = "foo"), str_replace(list.files(pattern = "foo"),pattern = "foo", "bob"))

下面是我使用简单的基本代码按顺序匹配和更改指定目录中的所有文件名的解决方案。

old_files <- list.files(path = ".", pattern="water_*.img$")


# Create df for new files
new_files <- data.frame()


for(i in 1:length(old_files)){
new_files <- append(paste0(path = ".", substr(old_files[i], 1,6),"water_",i,".img"), new_files)
}


new_files <- as.character(new_files)


# Copy from old files to new files


file.rename(from = old_files), to = as.vector(new_files)