空ディレクトリ削除するためのスクリプトを書いたのでメモしておく
dir_del.rb本体
#!/usr/bin/env ruby
# encoding: utf-8
#
require "find"
require 'optparse'
puts "削除"
# コマンドライン引数を解析してハッシュに格納する
options = {}
OptionParser.new do |parser|
# オプションを指定
parser.on('-m', "--mode モード選択: input(削除確認する) noinput(確認しない,初期値) ") {|v| options[:mode] = v}
parser.on('-d', "--directory ディレクトリ指定 ") {|v| options[:directory] = v}
parser.parse!(ARGV)
end
if options[:mode] && options[:mode] == "input"
input_mode = true
else
input_mode = false
end
if options[:directory]
directory_path = options[:directory]
else
puts "ディレクトリ指定してください"
raise
end
puts ARGV[0]
delete_dirs = []
Find.find(directory_path) do |path|
if FileTest.directory?(path)
if Dir.entries(path).join == "..."
if input_mode
print "#{path} を削除しますか? [Y(defalut)/N]"
input_value = gets.chomp
unless input_value == "N"
Dir.rmdir(path)
delete_dirs << path
end
else
Dir.rmdir(path)
end
end
end
end
使い方
問答無用で削除
$ dir_del.rb -d 削除対象ディレクトリ
確認しながら削除
$ dir_del.rb -m input -d 削除対象ディレクトリ