67

我在一个文件夹中有 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
4

3 回答 3

101

file.rename将重命名文件,它可以同时包含fromto名称的向量。

所以像:

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

可能会奏效。

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

我将抛开这个问题,“你为什么想要?” 因为您似乎在丢弃文件名中的信息,但大概该信息也包含在其他地方。

于 2012-05-25T17:45:19.897 回答
8

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

如果你在 Mac 上,它可以使用 applescript 在 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)
    }
}
于 2013-09-17T16:58:51.150 回答
3

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

file.rename(list.files(pattern = "foo"), str_replace(list.files(pattern = "foo"),pattern = "foo", "bob"))
于 2021-07-16T15:40:35.627 回答