How to rename files with a specific pattern in R?
filenames, r, rename
Solution
Based on your comments, one issue is that your first file isn't named "data.001" - it's named "data.1". Use this:
b <- sprintf("data%.4d.fcs", seq(a))
It prepends up to 3 0s (since it seems you have 1000+ files, this may be better) to indices < 1000, so that all names have the same width. If you really just want to see things like "data.001", then use `%.3d` in the command.
Problem
There are some .fcs files in a data.000X format (where X = 1, 2, 3...) in a directory. I want to rename every n file to the following format: exp.fcs (where exp is a text from a vector) if the file to be renamed is an .fcs file. in other words: I want to rename files to exp.txt, where exp is a text and not a consecutive letter(s) i.e. F, cA, K, etc. For example, from: ``` data.0001, data.0002, data.0003, data.0004, data.0005, data.0006... ``` to ``` textF_a.fcs, textF_b.fcs, textF_c.fcs, textVv_a.fcs, textVv_b.fcs, textVv_c.fcs ... ``` I tried to do it with file.rename(from, to) but failed as the arguments have different lengths (and I don't know what it means): ``` a <- list.files(path = ".", pattern = "data.*$") b <- paste("data", 1:1180, ".fcs", sep = "") file.rename(a, b) ```