我需要帮助将成千上万个.wav文件的文件名(例如“ 2018-10-26 17/11/00”)更改为“ 2018-10-26 17:11:00”,但还要从该小时中减去5小时文件,因此最终名称应为“ 2018-10-26 12:11:00” 我正在使用Rstudio来执行此操作。这些文件之前我只有十六进制数,代码是:
#Convert filenames to date and time of creation
library(tidyverse)
#Set directory to folder containing files
setwd("/Volumes/Informacion/Paisaje Jave/Grabadora 4, (cancha de football)")
#Change this to wherever your files are located
Audiomoth_Dir <- "/Volumes/Informacion/Paisaje Jave/Grabadora 4, (cancha de football)"
#Generate list of files present within the folder
file_list <-list.files(Audiomoth_Dir, pattern = "*.WAV", full.names = FALSE)
#Generate vector of creation dates and times
wav_file_info <- file.info(file_list)
new_names <- as.character(wav_file_info$mtime)
#Rename files
file.rename(from = file_list, to = str_c(new_names,".wav"))
file.rename()
谢谢!
在R2evans告诉我之后,我编写了以下新代码:
file_list <-list.files(Testfolder, pattern = "*.WAV", full.names = FALSE)
gsub(".wav", "", Testfolder)
as.POSIXct(gsub(".wav", "", Testfolder), format="%Y-%m-%d %H/%M/%S") - 3600*5
format(as.POSIXct(gsub(".wav", "", Testfolder), format="%Y-%m-%d %H/%M/%S") - 3600*5,"%Y-%m-%d %H:%M:%S.wav")
newTestfolder <- format(as.POSIXct(gsub(".wav", "", Testfolder), format="%Y-%m-%d %H/%M/%S") - 3600*5,"%Y-%m-%d %H:%M:%S.wav")
file.rename(from = Testfolder, to=newTestfolder)
答案 0 :(得分:0)
尽管我认为一种选择是使用适用的时区,但手动执行此操作也不难:
根据您的描述,一些假文件名:
filenames <- c("2018-10-26 17/11/00.wav", "2018-10-26 03/22/00.wav")
第一步是删除名称的非时间戳部分,在这种情况下,该部分应该只是文件扩展名:
gsub(".wav", "", filenames)
# [1] "2018-10-26 17/11/00" "2018-10-26 03/22/00"
现在,如果我们将其转换为POSIXt
个对象并减去魔法“ 5小时”:
as.POSIXct(gsub(".wav", "", filenames), format="%Y-%m-%d %H/%M/%S") - 3600*5
# [1] "2018-10-26 12:11:00 PDT" "2018-10-25 22:22:00 PDT"
我们现在可以根据需要使用format
重新引入文件名:
newfilenames <- format(as.POSIXct(gsub(".wav", "", filenames), format="%Y-%m-%d %H/%M/%S") - 3600*5,
"%Y-%m-%d %H:%M:%S.wav")
newfilenames
# [1] "2018-10-26 12:11:00.wav" "2018-10-25 22:22:00.wav"
然后,您只需将它们重命名为file.rename(filenames, newfilenames)
。