discussion / Acoustic Monitoring  / 28 June 2022

Renaming files with incorrect timestamps


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!

(Also posted on Stack Overflow).

Hi Carly,

On a Mac, I use "SetEXIFData" by Marc Vos to increment file or EXIF dates. You can then pull the file name from the date. I'm sure there are Windows and Linux equivalents.

However, what I would really like is a simple way to scale file times where I have the first date and I then know that the last time stamp was offset forward or backwards because of poor timekeeping by the device. Maybe there is something in R or Python that will do it.

I've already talked to Carly about this, but gonna just re-post my code here:



#makes a list of all the files
allFiles <- list.files(pattern = '.wav', full.names = T)

#turns the list into a data frame
allFiles_df <- data.frame(fullPath = allFiles)

#drops all the preceding stuff from the file path
allFiles_df$fileName <- basename(allFiles_df$fullPath)

#extracts anything formatted with 8 digits, then 6 digits with an underscore between (i.e. wildlife acoustics style timestamps). If you only have 2 digit years replace the {8} with {6}
allFiles_df$timestamp_str <- str_extract(allFiles_df$fileName, '[0-9]{8}_[0-9]{6}')

#turns the character string into a timestamp
allFiles_df$timestamp <- lubridate::ymd_hms(allFiles_df$timestamp_str)

#adds three hours to the timestamp
allFiles_df$timestamp_new <- allFiles_df$timestamp + hours(3)

Just posting the link to the Bioacoustics Stack Exchange post where I asked this as well, some other people gave code as well (though I haven't had time to try it as it has been a holiday weekend here in the US).