Skip to content

Instantly share code, notes, and snippets.

@rhine3
Last active June 24, 2020 22:28
Show Gist options
  • Save rhine3/fe57c7b8885efedfbae9ead9161003af to your computer and use it in GitHub Desktop.
Save rhine3/fe57c7b8885efedfbae9ead9161003af to your computer and use it in GitHub Desktop.
Convert hex filenames to decimal date/time using R
top_dir <- "/Users/tessa/Desktop/moth-tests/M10_0001_test0"
for (file in list.files(top_dir)){
old_filepath <- paste(top_dir, file, sep="/")
print(paste("Old filepath:", old_filepath))
hex_code <- tools::file_path_sans_ext(basename(old_filepath))
seconds <- strtoi(hex_code, base=16)
true_time <- as.POSIXct(seconds, origin="1970-01-01")
formatted <- strftime(true_time, "%Y%m%d_%H%M%S")
new_filepath <- paste(top_dir, "/", formatted, ".WAV", sep="")
print(paste("New filepath:", new_filepath))
file.rename(from=old_filepath, to=new_filepath)
break() #check this works with one file before renaming all files!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment