0

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!

Carly Batist
  • 101
  • 3

1 Answers1

0

It sounds like you want to add 8203 days, 9 hours, and 30 minutes to the date/time implied in each file name. Try this. Assumes you are updating all the files in your current directory.

library(lubridate)
library(stringr)     
              
  files <- list.files(getwd())     
  
  for(f in 1:length(files)){
      x <- files[f]
      orig <- ymd_hms(paste0(substr(x, 8, 11),"-",substr(x,12,13),"-",substr(x,14,15)," ",
                             substr(x, 17,18),":",substr(x, 19,20),":", substr(x, 21,22)))
      y <- as.character(orig + days(8203) + hours(9) + minutes(30))
      new <- paste0("swift1_", str_replace(str_remove_all(y, "-|:"), " ","_"),".wav")
      file.rename(from = x, to = new)
      
  }
  

First we get all the file names from the directory. Then for each file, we extract the date components, combine, convert to a date, add the adjustment, then break the date components back out to form a new file name. Finally, we rename the original file.

stomper
  • 1,252
  • 1
  • 7
  • 12