Skip to content

Instantly share code, notes, and snippets.

@daz
Last active September 2, 2022 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daz/9c214125341ce7a3dc3909a1126297c8 to your computer and use it in GitHub Desktop.
Save daz/9c214125341ce7a3dc3909a1126297c8 to your computer and use it in GitHub Desktop.
Organize DJI H20T images into folders
#!/usr/bin/env ruby
# organize-h20t.rb [files]
# https://gist.github.com/daz/9c214125341ce7a3dc3909a1126297c8
# Organize H20T images, videos, and captions into folders by camera types, i.e. S, T, W, Z
# Creates a folder for each camera type, and moves the files into their respective folder
# Usage:
# cd DJI_202208071450_002
# ./organize-h20t.rb DJI_20220807145439_0001_S.MP4 DJI_20220807145439_0001_T.MP4 DJI_20220807145439_0001_W.MP4 DJI_20220807145439_0001_Z.MP4
require 'fileutils'
FILENAME_REGEX = /^([A-Za-z0-9]+)_(\d{14})_(\d{4})_(S|T|W|Z)$/
ARGF.each do |path|
path = path.strip
extension = File.extname(path)
filename = File.basename(path, extension)
dir = File.expand_path("..", path)
match = filename.match(FILENAME_REGEX)
next if !File.file?(path) || !match
_, prefix, timestamp, number, camera = match.to_a
new_dir = File.join(dir, camera)
FileUtils.mkdir_p(new_dir) unless File.directory?(new_dir)
FileUtils.mv(path, new_dir)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment