如何使用R中移动多个顺序文件名(WAV文件,从生物声监视)中移动时间戳

发布于 2025-02-11 03:08:21 字数 459 浏览 1 评论 0原文

我部署了被动声记录器来检测动物的电话,但在我的一种设备上,日期&时间设置被搞砸了,并恢复为默认设置(2000年1月1日)。然后记录了一个月的设备,编写了Day&时间盖章。例如,文件名是swift1_20000101_020000.wav(devicename_yyyymmdd_hhmmss)。现在,我非常不想手动重命名2000多个个人文件。

我知道实际的开始日期&从我的字段注释中的时间,我想知道是否有一种方法可以输入实际的开始日期/时间并将所有文件移开。因此,Swift1_20000101_000000将成为Swift1_20220617_093000,Swift1_20000101_002000将变为Swift1_20220617_095000,等等。

有什么想法吗?我知道您可以使用file.rename(),paste0()等重命名文件,但是我需要在目录中的所有文件上迭代一个函数,并且我找不到可以做到的东西。任何想法或想法都将不胜感激!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2025-02-18 03:08:21

听起来您想将8203天,9小时和30分钟添加到每个文件名中隐含的日期/时间。尝试一下。假设您正在更新当前目录中的所有文件。

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)
      
  }
  

首先,我们从目录中获取所有文件名。然后,对于每个文件,我们提取日期组件,组合,转换为日期,添加调整,然后将日期组件返回以形成新文件名。最后,我们重命名原始文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文