Skip to content

Instantly share code, notes, and snippets.

@IcyApril
Last active September 8, 2015 13:22
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 IcyApril/9b875d2c75414166c361 to your computer and use it in GitHub Desktop.
Save IcyApril/9b875d2c75414166c361 to your computer and use it in GitHub Desktop.
A Ruby script to extract WordPress uploads without resized images.
#!/usr/local/bin/ruby -w
# A Ruby script to extract WordPress uploads, excluding resized images, into a single directory.
# File names will be prepended with the directory #string.
# RUN: ruby wpmediaexport.rb >> wpmediaexport.sh
# THEN REVIEW/RUN: ./wpmediaexport.sh
# Generated shell script will be in wpmediaexport.sh.
# Cd into the uploads directory in WordPress before running.
# Make sure you create an empty img directory prior to running wpmediaexport.sh
# wpmediaexport.sh needs permission to run, thus you might need to run chmod +x wpmediaexport.sh.
puts "#!/bin/bash"
files = Dir.glob("**/*")
files.each do |item|
next if item == '.' or item == '..'
next if item == 'wpmediaexport.rb' or item == 'wpmediaexport.sh'
noext = File.basename(item)
base = File.basename(item,File.extname(item))
afterdash = base.partition('-').last
dirname = File.dirname(item)
dirnamestripped = 'img/'+dirname.gsub('/', '').gsub('.', '')
if File.file?(item)
if afterdash.empty?
puts "cp "+item+" "+dirnamestripped+noext
else
if afterdash !~ /[0-9]+x[0-9]+/
puts "cp "+item+" "+dirnamestripped+noext
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment