Skip to content

Instantly share code, notes, and snippets.

@ddd1600
Last active February 27, 2018 20:16
Show Gist options
  • Save ddd1600/75ea57e27298976849e6b70c06e59544 to your computer and use it in GitHub Desktop.
Save ddd1600/75ea57e27298976849e6b70c06e59544 to your computer and use it in GitHub Desktop.
zip individual files inside of folder
require 'shellwords'
require 'highline/import'
target_ext = ask("file extension to target (excluding the period)?")
output_ext = ask("output file extension? press enter to default to zip")
output_ext = output_ext.blank? ? "zip" : output_ext
files = Dir.glob("*.#{target_file_extension}")
files.each do |f|
puts "-----\nworking with '#{f}'"
f = File.basename(f)
basename = f.split(".")[0..-2].join
puts "extensionless basename = '#{basename}'"
shellsafebasename = Shellwords.escape(basename)
puts "escaped path for shell = '#{shellsafebasename}'"
shellcmd = "zip -r #{shellsafebasename}.#{output_ext} #{Shellwords.escape(f)}"
puts "trying to run this command: '#{shellcmd}'"
`#{shellcmd}`
end# of files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment