Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Last active April 16, 2019 14:34
Show Gist options
  • Save ElectricCoffee/5a0c49acb9da96f94373a37d083b5fcf to your computer and use it in GitHub Desktop.
Save ElectricCoffee/5a0c49acb9da96f94373a37d083b5fcf to your computer and use it in GitHub Desktop.
ruby implementation of my old cleanup.bash file.
#! /usr/bin/ruby
require './prompt'
# Exit early if a file name hasn't been provided
if ARGV.length < 1 then
puts "Please provide an identifier"
exit
end
# Gets all the strings that include the provided identifier
matches = Dir.entries('.').find_all { |str| str.include? ARGV[0] }
# If no identifiers match, just exit
exit if matches.count == 0
# Shows the user which files it found
matches.each do |file|
puts "Found #{file}."
end
# Prompt the user for whether or not they wish to delete the files
prompt("Do you wish to delete #{matches.count} files? [Y/n] ") do |answer|
answer.downcase!
case answer
when "y", "yes" then
# Iterate through the array of matches and remove them
matches.each do |file|
if !File.directory?(file) then
File.delete file
puts "#{file} has been deleted."
end
end
else
puts "Okay, nothing's been deleted."
end
end
# /usr/bin/ruby
# Convenience function to deal with user prompts
# Prints `text` to the terminal, then passes the user's response into the block
def prompt(text, src = STDIN)
print text
input = src.gets.chomp
yield input
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment