Renaming photographs with R

I have a folder of photographs. I want to rename them in R like this:

ProjectName-YYYYMMDDD_HHMMSS

At the end of the name, I want to add a lower case letter for all photographs that were taken within the same second. for example:

ProjectName-20220826_153324a

ProjectName-20220826_153324b

ProjectName-20220826_153324c

ProjectName-20220826_153325

ProjectName-20220826_153326a

ProjectName-20220826_153326b

Thank you for your help!

I am using .txt files here, but the process should be the same for photographs.

Here is a look at six text files in your_folder.
image

The steps below will read in the file paths, gather timestamp info via file.info, arrange and group by timestamps to determine which letter to add, and ultimately construct the new filenames.

library(tidyverse)

# read your files
filepaths = list.files('your_folder', full.names = T) # full file paths
filenames = list.files('your_folder') # file names only

# get file info
file_data = lapply(filepaths, file.info) |>
  bind_rows() |>
  # add a column name for filename
  bind_cols(
    tibble(filename = filenames)
    ) |>
  rownames_to_column('filepath_original') |>
  arrange(mtime) |>
  # group by files with the same timestamp (mtime)
  # and generate the associated letter (where appropriate)
  group_by(mtime) |>
  mutate(max_rows = max(row_number())) |>
  mutate(letter = ifelse(max_rows > 1,
                         letters[row_number()],
                         '')
         ) |>
  ungroup() |>
  # clean up the time string
  mutate(time = str_replace_all(mtime, '-', ''),
         time = str_replace_all(time, ':', ''),
         time = str_replace(time, ' ', '_')) |>
  # construct the new file path
  mutate(filename_new = paste0('ProjectName_', time, letter)) |>
  mutate(filepath_new = str_replace(filepath_original, filename, ''),
         filepath_new = paste0(filepath_new, filename_new, '.txt')
         ) |>
  select(filepath_original, filepath_new)

Executing this code produces...
image

Finally, use file.rename() to rename the files in the folder.

# rename in folder
file.rename(from = file_data$filepath_original,
            to = file_data$filepath_new)

This ultimately results in the six newly named files below.
image

Thank you for the detailed reply
I got several error messages..:

  rownames_to_column('filepath_original') 
Error in rownames_to_column("filepath_original") : 
  is.data.frame(df) is not TRUE
>   arrange(mtime) 
Error in arrange(mtime) : object 'mtime' not found
>   # group by files with the same timestamp (mtime)
>   # and generate the associated letter (where appropriate)
>   group_by(mtime) 
Error in group_by(mtime) : object 'mtime' not found
>   mutate(max_rows = max(row_number())) 
Error: `n()` must only be used inside dplyr verbs.
Run `rlang::last_error()` to see where the error occurred.
>   mutate(letter = ifelse(max_rows > 1,
+                          letters[row_number()],
+                          '')
+          ) 
Error in ifelse(max_rows > 1, letters[row_number()], "") : 
  object 'max_rows' not found
>   ungroup() 
Error in UseMethod("ungroup") : 
  no applicable method for 'ungroup' applied to an object of class "NULL"
>   # clean up the time string
>   mutate(time = str_replace_all(mtime, '-', ''),
+          time = str_replace_all(time, ':', ''),
+          time = str_replace(time, ' ', '_')) 
Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement),  : 
  object 'mtime' not found
>   # construct the new file path
>   mutate(filename_new = paste0('CETI2022_', time, letter)) 
Error in paste0("CETI2022_", time, letter) : object 'letter' not found
>   mutate(filepath_new = str_replace(filepath_original, filename, ''),
+          filepath_new = paste0(filepath_new, filename_new, '.txt')
+          ) 
Error in type(pattern) : object 'filename' not found
>   select(filepath_original, filepath_new)
Error in select(filepath_original, filepath_new) : 
  object 'filepath_original' not found

It looks like each line is executing individually instead of together. A couple things to try.

  • If using RStudio, select all of the code and click "Run" at the top.
  • The code I provided uses the native pipe ( |> ). I'm not sure what version of R you are using , but perhaps try changing all native pipes ( |> ) to the pipe from the magrittr package ( %>% ).

@Yalym :
what is the contents of filepaths and filenames: how many elements e.g. ?

Yes it was %>%
my bad, thank you!
The only problem left is that it's taking the Date Modified instead of Date taken
any ideas?
thanks

The function file.info returns three columns - mtime, ctime, and atime. I chose to use mtime for this example, but you may need either ctime or atime.

When I try ctime , and atime some of the files are being deleted from the folder

After looking further at some documentation, mtime is the time the file was last modified. Therefore, if you are first copying image files from a device or memory card to a folder location and then trying to rename, then mtime will likely be the same for all files.

I assume that if your filepath points directly to the device/memory card, then the mtime will be the time the photo was created (i.e. picture taken), but I don't know this for sure. However, if you try to rename the file on the device/memory card, then there is a chance the image could be erased. If you do try to point directly to the device/memory card, I would probably make copies of all the files first.

Thank you so much for putting the time and effort to help me

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.