I deployed passive acoustic recorders to detect animal calls but on one of my devices the date & time settings got messed up and reverted to the default (Jan 1, 2000). And then the device recorded for a month, writing 20-min files that are day & time stamped. e.g., file name is swift1_20000101_020000.wav (deviceName_YYYYMMDD_HHMMSS). And now I reeeeally don't want to have to manually rename 2000+ individual files.

I know the actual start date & time from my field notes and I'm wondering if there's a way to input that actual start date/time and have all the files shift off of that. So swift1_20000101_000000 would become swift1_20220617_093000, swift1_20000101_002000 would become swift1_20220617_095000, and so on in some sort of loop.

Any ideas? I know you can rename files with file.rename(), paste0(), etc but I would need a function that iterated on all files within the directory sequentially and I haven't been able to find something that will do it. Any thoughts or ideas would be much appreciated!

  • $\begingroup$ Selene Fregosi told me to post it here $\endgroup$ Jun 28, 2022 at 13:38
  • 4
    $\begingroup$ @Thejasvi While you could argue that this question is better suited for a different platform, I think this is a very common problem in bioacoustic data logging and if this site is going to be the leader for bioacoustic-related questions, I would argue that it is an appropriate question to ask here. $\endgroup$ Jun 28, 2022 at 15:09
  • 1
    $\begingroup$ Have deleted my comments seeing the discussion at the Meta play out! There is indeed sense in having field-specidic programming questions. $\endgroup$
    – Thejasvi
    Jun 30, 2022 at 8:03

1 Answer 1


This is a common problem and we run into it often. I have mocked up some example R code to show how you can handle this problem in R as described. You can also build a new folder structure and use this (or similar) code to reorganize files.


# set deployment date/time
datetime_deployment= ymd_hms("20220617_093000")

# list all the files that need to be changed
# files<-dir_ls("path/to/files",glob = "*wav")


# We will use this data frame for organizing the new paths to which 
# we will rename your files
                filename=basename(files)) %>%
        # extract the file datetimes
        # find the difference between your files and the deployment datetime
        # add the max of the differences to the file date time (this
        # assumes that the deployment datetime is after all
        # of the file datetimes)
        # extract the sensor_name from the filename (for your usecase
        # this regex is way overkill but it works for other
        # prefixes and datetime formats)
        # get the file extention (useful if you have more than 1)
        # build new path

# check for duplicates and fix if there are, otherwise you will
# overwrite them when you move
dat %>% 
    group_by(new_path) %>%
    tally %>% 

# you can copy the files (will not change the originals) or you can
# move the files (the originals disappear).  Use copy=TRUE
# if you are unsure if you have the correct paths and want to be able
# to go back to the originals
copy = TRUE

    # slow and you will get a copy of the file (but it is safe if you
    # have any mistakes in the new path you will still have
    # the old data)
    file_copy(dat$path,dat$new_path, overwrite = F)
    # WAY faster but distructive (essentially will rename the files)
  • $\begingroup$ Awesome, thank you so much!! Will give this go and report back... $\endgroup$ Jun 28, 2022 at 13:37
  • $\begingroup$ @CarlyBatist did this code work to solve your problem? $\endgroup$ Jul 5, 2022 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.