如何使用R重命名文件?

时间:2022-12-01 10:32:55

I have over 700 files in one folder named as: files from number 1 to number9 are named for the first month:

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

water_200101_01.img  
water_200101_09.img  

files from number 10 to number30 are named:

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

water_200101_10.img
water_200101_30.img

And so on for the second month: files from number 1 to number9 are named:

第二个月也是如此:从1号到9号的文件被命名为:

water_200102_01.img  
water_200102_09.img  

files from number 10 to number30 are named:

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

water_200102_10.img
water_200102_30.img 

How can I rename them without making any changes to the files. just change the nams, for example

如何在不修改文件的情况下重命名它们。例如,只需更改nams

water_1
water_2
...till...
water_700

2 个解决方案

#1


33  

file.rename will rename files, and it can take a vector of both from and to names.

文件。rename将重命名文件,它可以取一个来自和到名称的向量。

So something like:

所以类似:

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

might work.

可能会奏效。

If care about the order specifically, you could either sort the list of files that currently exist, or if they follow a particular pattern, just create the vector of filenames directly (although I note that 700 is not a multiple of 30).

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

I will set aside the question, "why would you want to?" since you seem to be throwing away information in the filename, but presumably that information is contained elsewhere as well.

我将把“您为什么要这样做?”这个问题放在一边,因为您似乎在丢弃文件名中的信息,但假定该信息也包含在其他地方。

#2


4  

I wrote this for myself. It is fast, allows regex in find and replace, and supports "trial runs".

这是我自己写的。它速度很快,允许regex进行查找和替换,并支持“试运行”。

If you are are on a mac, it can use applescript to pick out the current folder in the Finder as a target folder.

如果你在mac电脑上,它可以使用applescript来将查找器中的当前文件夹作为目标文件夹。

umxRenameFile <- function(baseFolder = "Finder", findStr = NA, replaceStr = NA, listPattern = NA, test = T, overwrite = F) {
    # uppercase = u$1
    if(baseFolder == "Finder"){
        baseFolder = system(intern = T, "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)
    }
    if(is.na(listPattern)){
        listPattern = findStr
    }
    a = list.files(baseFolder, pattern = listPattern)
    message("found ", length(a), " possible files")
    changed = 0
    for (fn in a) {
        findB = grepl(pattern = findStr, fn) # returns 1 if found
        if(findB){
            fnew = gsub(findStr, replace = replaceStr, fn) # replace all instances
            if(test){
                message("would change ", fn, " to ", 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(paste(baseFolder, fn, sep = ""), paste(baseFolder, fnew, sep = ""))
                    changed = changed + 1;
                }
            }
        }else{
            if(test){
                # message(paste("bad file",fn))
            }
        }
    }
    message("changed ", changed)
}

#1


33  

file.rename will rename files, and it can take a vector of both from and to names.

文件。rename将重命名文件,它可以取一个来自和到名称的向量。

So something like:

所以类似:

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

might work.

可能会奏效。

If care about the order specifically, you could either sort the list of files that currently exist, or if they follow a particular pattern, just create the vector of filenames directly (although I note that 700 is not a multiple of 30).

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

I will set aside the question, "why would you want to?" since you seem to be throwing away information in the filename, but presumably that information is contained elsewhere as well.

我将把“您为什么要这样做?”这个问题放在一边,因为您似乎在丢弃文件名中的信息,但假定该信息也包含在其他地方。

#2


4  

I wrote this for myself. It is fast, allows regex in find and replace, and supports "trial runs".

这是我自己写的。它速度很快,允许regex进行查找和替换,并支持“试运行”。

If you are are on a mac, it can use applescript to pick out the current folder in the Finder as a target folder.

如果你在mac电脑上,它可以使用applescript来将查找器中的当前文件夹作为目标文件夹。

umxRenameFile <- function(baseFolder = "Finder", findStr = NA, replaceStr = NA, listPattern = NA, test = T, overwrite = F) {
    # uppercase = u$1
    if(baseFolder == "Finder"){
        baseFolder = system(intern = T, "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)
    }
    if(is.na(listPattern)){
        listPattern = findStr
    }
    a = list.files(baseFolder, pattern = listPattern)
    message("found ", length(a), " possible files")
    changed = 0
    for (fn in a) {
        findB = grepl(pattern = findStr, fn) # returns 1 if found
        if(findB){
            fnew = gsub(findStr, replace = replaceStr, fn) # replace all instances
            if(test){
                message("would change ", fn, " to ", 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(paste(baseFolder, fn, sep = ""), paste(baseFolder, fnew, sep = ""))
                    changed = changed + 1;
                }
            }
        }else{
            if(test){
                # message(paste("bad file",fn))
            }
        }
    }
    message("changed ", changed)
}