How to Rename Files Using R

How do I rename files using R?

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

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).

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.

File renaming using R

Given that you are in the right working directory (otherwise set it with setwd(""), you can change file names with:

from1 <- c("test_file.csv", "plot1.svg")
to1 <- c("test.csv", "plot.svg")

file.rename(from1, to1)

But make sure that you are in the right directory and that the files exist (which you can do with list.files or file.exists.

How to rename files using R?

I don't think you need to do this with sapply at all. (This will bypass the problem you are having where the arguments to file.rename and sub should have been X.) Try this instead:

filez <- list.files()
file.rename(from=filez, to=sub(pattern="_001", replacement="", filez))

Rename files in R

EDIT 2: The solution here was for the OP to change the Region settings in Control Panel, setting format to be in Serbian(Latin, Serbia).

EDIT 1: See the comments: the OP is on a Windows machine. Here the problem is that list.files() (and presumably dir(), since they call the same .Internal) is converting the non ASCII filenames to ASCII, but Windows is expecting file.exists() to send it the unicode filenames, (and presumably also file.rename())

Try:

file.rename(gsub("c", "č", files), paste0("novi_", seq_along(files, ".xlsx"))
# could work, but it didn't for `file.exists()`

Original answer:

setwd(<your path>)
(files <- list.files())
# [1] "račun 1.xlsx" "račun 2.xlsx" "račun 3.xlsx" "račun 4.xlsx" "račun 5.xlsx [6] "račun 6.xlsx"
file.rename(files, paste0("novi_", seq_along(files, ".xlsx"))
# [1] TRUE TRUE TRUE TRUE TRUE TRUE

The fact that you specified a path in list.files(), suggests that you're not in the correct directory

Renaming files in directory based on filename using R

I suggest you do things in phases, partly to make sure it's working correctly (testing), partly because it's easy to 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

If the new names ($FileName2) look good, then

ign <- mapply(file.rename, FilesDf$FileName, FilesDf$FileName2)

should work.

(I was initially distracted by $FileName vs $Filename, and missed the second ...)


Data:

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"))

rename file(s) in folder by list in R

here's one approach:

replacement <- names(list_wrong_names)
names(replacement) <- names(list_wrong_names)

for (wn in names(list_wrong_names)){
w_cont <- list_wrong_names[[wn]]
for (cn in names(list_correct_names)){
c_cont <- list_correct_names[[cn]]
if (identical(w_cont, c_cont))
replacement[[wn]] <- cn
}
}


file.rename(names(replacement), replacement)

Renaming files and directories with the same pattern in R

No loops required.

metadata <- read.table(header=T, stringsAsFactors=F, text="
old new
wh_ah108090 gsmp01358_108090
wh_ah108091 gsmp01359_108091
wh_ah108092 gsmp01360_108092
wh_ah108093 gsmp01361_108093
wh_ah108096 gsmp01362_108096
wh_ah108102 gsmp01363_108102
wh_ah108106 gsmp01364_108106")
metadata$new2 <- sprintf("gsmp%05d_%s",
1357L + seq_len(nrow(metadata)), # 1357 can be anything?
gsub("\\D", "", metadata$old))
metadata
# old new new2
# 1 wh_ah108090 gsmp01358_108090 gsmp01358_108090
# 2 wh_ah108091 gsmp01359_108091 gsmp01359_108091
# 3 wh_ah108092 gsmp01360_108092 gsmp01360_108092
# 4 wh_ah108093 gsmp01361_108093 gsmp01361_108093
# 5 wh_ah108096 gsmp01362_108096 gsmp01362_108096
# 6 wh_ah108102 gsmp01363_108102 gsmp01363_108102
# 7 wh_ah108106 gsmp01364_108106 gsmp01364_108106

file.rename(metadata$old, metadata$new2) # should do it


Related Topics



Leave a reply



Submit