Skip to content

Instantly share code, notes, and snippets.

@marutter
Forked from tjmahr/sort.R
Created September 25, 2018 01:06
Show Gist options
  • Save marutter/f64a697a308dddfea5da265c79cb8560 to your computer and use it in GitHub Desktop.
Save marutter/f64a697a308dddfea5da265c79cb8560 to your computer and use it in GitHub Desktop.
sorting photos
dir <- "F:/Tristan/OneDrive/Pictures/Camera Roll"
ps <- list.files(dir, "*.jpg", full.names = TRUE)
head(ps)
library(exiftoolr)
library(dplyr)
exif <- exif_read(head(ps, 2500), c("SourceFile", "CreateDate" ))
exif <- exif %>%
filter(!is.na(CreateDate)) %>%
mutate(
CreateDate = lubridate::ymd_hms(CreateDate),
Year = lubridate::year(CreateDate),
Month = sprintf("%02.f", lubridate::month(CreateDate))) %>%
filter(!is.na(Month), !is.na(Year))
exif
dates <- distinct(exif, Year, Month)
for (x in seq_along(dates$Month)) {
row <- dates[x, ]
year_dir <- fs::path_join(c(dir, row$Year))
month_dir <- fs::path_join(c(year_dir, row$Month))
fs::dir_create(year_dir)
fs::dir_create(month_dir)
}
for (x in seq_along(exif$SourceFile)) {
row <- exif[x, ]
year_dir <- fs::path_join(c(dir, row$Year))
month_dir <- fs::path_join(c(year_dir, row$Month))
new_path <- fs::path_join(c(month_dir, basename(row$SourceFile)))
fs::file_move(row$SourceFile, new_path)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment