Answers.Body: <p>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.</p>
  <pre><code>library(stringr)
  library(lubridate)
  library(fs)
  library(dplyr)
  
  # set deployment date/time
  datetime_deployment= ymd_hms(&quot;20220617_093000&quot;)
  
  # list all the files that need to be changed
  # files&lt;-dir_ls(&quot;path/to/files&quot;,glob = &quot;*wav&quot;)
  
  files&lt;-c(&quot;path/to/file/swift1_20000101_020000.wav&quot;,
           &quot;path/to/file/swift1_20000101_022000.wav&quot;)
  
  # We will use this data frame for organizing the new paths to which 
  # we will rename your files
  dat&lt;-data.frame(path=files,
                  filename=basename(files)) %&gt;%
      mutate(
          # extract the file datetimes
          datetime=ymd_hms(str_extract(filename,&quot;[0-9]{8}_[0-9]{6}&quot;)),
          # find the difference between your files and the deployment datetime
          diff=datetime_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)
          datetime_new=datetime+max(diff),
          # extract the sensor_name from the filename (for your usecase
          # this regex is way overkill but it works for other
          # prefixes and datetime formats)
          sensor_name=str_extract(filename,
               '^.*?(?=((_D[0-9]{8}(\\([0-9]\\)|_))?(_|-)?D?[0-9]{8}(T|_)[0-9]{6}))'),
          # get the file extention (useful if you have more than 1)
          file_ext=path_ext(path),
          # build new path
          new_path=file.path(dirname(path),
                             paste0(sensor_name,&quot;_&quot;,
                                    format(datetime_new, 
                                           &quot;%Y%m%d_%H%M%S&quot;),&quot;.&quot;, 
                                           file_ext)))
  
  # check for duplicates and fix if there are, otherwise you will
  # overwrite them when you move
  dat %&gt;% 
      group_by(new_path) %&gt;%
      tally %&gt;% 
      filter(n&gt;1)
  
  # 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
  
  
  if(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)
  }else{
      # WAY faster but distructive (essentially will rename the files)
      file_move(dat$path,dat$new_path)
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-28T13:37:25.677Z
Answers.Text: Awesome, thank you so much!! Will give this go and report back...
Answers.UserDisplayName: Carly Batist
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-05T14:41:42.520Z
Answers.Text: @CarlyBatist did this code work to solve your problem?
Answers.UserDisplayName: Abram Fleishman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-28T04:42:49.327Z
Answers.LastActivityDate: 2022-06-28T04:48:25.543Z
Answers.LastEditDate: 2022-06-28T04:48:25.543Z
Answers.UserDisplayName: Abram Fleishman
Body: <p>I deployed passive acoustic recorders to detect animal calls but on one of my devices the date &amp; 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 &amp; 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.</p>
  <p>I know the actual start date &amp; 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.</p>
  <p>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!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-06-28T13:38:33.443Z
Comments.Text: Selene Fregosi told me to post it here
Comments.UserDisplayName: Carly Batist
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-06-28T15:09:19.773Z
Comments.Text: @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.
Comments.UserDisplayName: Abram Fleishman
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-06-30T08:03:02.063Z
Comments.Text: Have deleted my comments seeing the discussion at the Meta play out! There is indeed sense in having field-specidic programming questions.
Comments.UserDisplayName: Thejasvi
Tags: <files><r>
Title: How to shift timestamps in multiple sequential file names (wav files, from bioacoustic monitoring) using R
ThreadText: # How to shift timestamps in multiple sequential file names (wav files, from bioacoustic monitoring) using R
  
  (Asked by: username_0 on 2022-06-28)
  
  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!
  
  **Tags:** files, r
  
  > **Comments:**
  > **username_0**: Selene Fregosi told me to post it here  
  > **username_1**: @username_2 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.  
  > **username_2**: Have deleted my comments seeing the discussion at the Meta play out! There is indeed sense in having field-specidic programming questions.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-06-28. Score: 10 | Accepted answer
  
  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.
  library(stringr)
  library(lubridate)
  library(fs)
  library(dplyr)
  
  # 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")
  
  files<-c("path/to/file/swift1_20000101_020000.wav",
           "path/to/file/swift1_20000101_022000.wav")
  
  # We will use this data frame for organizing the new paths to which 
  # we will rename your files
  dat<-data.frame(path=files,
                  filename=basename(files)) %>%
      mutate(
          # extract the file datetimes
          datetime=ymd_hms(str_extract(filename,"[0-9]{8}_[0-9]{6}")),
          # find the difference between your files and the deployment datetime
          diff=datetime_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)
          datetime_new=datetime+max(diff),
          # extract the sensor_name from the filename (for your usecase
          # this regex is way overkill but it works for other
          # prefixes and datetime formats)
          sensor_name=str_extract(filename,
               '^.*?(?=((_D[0-9]{8}(\\([0-9]\\)|_))?(_|-)?D?[0-9]{8}(T|_)[0-9]{6}))'),
          # get the file extention (useful if you have more than 1)
          file_ext=path_ext(path),
          # build new path
          new_path=file.path(dirname(path),
                             paste0(sensor_name,"_",
                                    format(datetime_new, 
                                           "%Y%m%d_%H%M%S"),".", 
                                           file_ext)))
  
  # check for duplicates and fix if there are, otherwise you will
  # overwrite them when you move
  dat %>% 
      group_by(new_path) %>%
      tally %>% 
      filter(n>1)
  
  # 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
  
  
  if(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)
  }else{
      # WAY faster but distructive (essentially will rename the files)
      file_move(dat$path,dat$new_path)
  }
  
  > **Comments:**
  > **username_0**: Awesome, thank you so much!! Will give this go and report back...  
  > **username_1**: @CarlyBatist did this code work to solve your problem?  
  
  ---
  
  