Novice_stat
Novice_stat

Reputation: 45

How to rename mutliples files in r

I need to convert over 100 images name into a format like: SITE_T001_L001.jpg, where Site is CGS1, T= TUBES, L= image number.

All those images are contain into a single file named CGS1 (the site), subdivided by file named accordingly to their tubes number. Then the images are organised by date. This order represents the image number. The first one is 1, the second one is two.(the alpahabetic order is not correct)

here, I have a graphical representation: enter image description here I found how to do it manually in R

file.rename("Snap_029.jpg",
        paste("CGS1","T001","L003", ".jpg", sep = "_"))

but is there anyway to automate it with a loop?

In more details - as requested in the response: I have this series of input filenames (including leading path)- ordered by dates of modification (important).

file_list
[1] "CGS1/1/Snap_001.jpg" "CGS1/1/Snap_002.jpg" "CGS1/1/Snap_005.jpg" "CGS1/2/Snap_006.jpg" "CGS1/2/Snap_007.jpg" "CGS1/2/Snap_082.jpg"

I am looking to modify the name of each images following the main folder CGS1, the subfolder from T001 to T002, and following the date of modification from L001 to L003 as this output filenames

new_file_list
 [1] "CGS1_T001_L001.jpg" "CGS1_T001_L002.jpg" "CGS1_T001_L003.jpg" "CGS1_T002_L001.jpg" "CGS1_T002_L002.jpg" "CGS1_T002_L003.jpg"

Upvotes: 1

Views: 193

Answers (1)

r2evans
r2evans

Reputation: 160407

Try this:

file_list <- list.files(path = "...", recursive = TRUE, pattern = "\\.jpg$")
### for testing
file_list <- c(
  "CGS1/1/Snap_001.jpg", "CGS1/1/Snap_005.jpg", "CGS1/1/Snap_002.jpg",
  "CGS1/2/Snap_006.jpg", "CGS1/2/Snap_007.jpg", "CGS1/2/Snap_0082.jpg"
)

spl <- strsplit(file_list, "[/\\\\]")
# ensure that all files are exactly two levels down
stopifnot(all(lengths(spl) == 3))

m <- do.call(rbind, spl)
m
#      [,1]   [,2] [,3]           
# [1,] "CGS1" "1"  "Snap_001.jpg" 
# [2,] "CGS1" "1"  "Snap_005.jpg" 
# [3,] "CGS1" "1"  "Snap_002.jpg" 
# [4,] "CGS1" "2"  "Snap_006.jpg" 
# [5,] "CGS1" "2"  "Snap_007.jpg" 
# [6,] "CGS1" "2"  "Snap_0082.jpg"

From this, we'll update the second/third columns to be what you expect.

# either one (not both), depending on if you are guaranteed integers
m[,2] <- sprintf("T%03.0f", as.integer(m[,2]))
# ... or if you may have non-numbers
m[,2] <- paste0("T", strrep("0", max(0, 3 - nchar(m[,2]))), m[,2])

# since we really don't care about 'Snap_001.jpg' (etc), we can discard the third column
new_file_list <- apply(m[,1:2], 1, paste, collapse = "_")
# back-street way of applying sequences to each CGS/T combination while preserving order
for (prefix in unique(new_file_list)) {
  new_file_list[new_file_list == prefix] <- sprintf("%s_L%03d.jpg",
                                                    new_file_list[new_file_list == prefix],
                                                    seq_len(sum(new_file_list == prefix)))
}
new_file_list
# [1] "CGS1_T001_L001.jpg" "CGS1_T001_L002.jpg" "CGS1_T001_L003.jpg"
# [4] "CGS1_T002_L001.jpg" "CGS1_T002_L002.jpg" "CGS1_T002_L003.jpg"

Now it's a matter of renaming. Note that this will move all files into the current directory.

file.rename(file_list, new_file_list)

Upvotes: 2

Related Questions