Skip to content

Instantly share code, notes, and snippets.

@nazoking
Created January 6, 2012 03:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazoking/1568834 to your computer and use it in GitHub Desktop.
Save nazoking/1568834 to your computer and use it in GitHub Desktop.
cygwinからゴミ箱に捨てる
#!/bin/ruby
require 'Win32API'
module Cygwin
# cygwin環境であればtrue
def cygwin?
RUBY_PLATFORM =~ /-cygwin$/
end
# TODO コロン(:)などwindowsでファイル名に使えない文字が名前に入ってるファイルをうまく変換できません助けて!
# http://www.ruby-lang.org/ja/old-man/html/Win32API.html
PATH_TO_WIN32_FULL = Win32API.new('cygwin1.dll', 'cygwin_conv_to_full_win32_path', 'PP', 'I')
def cygpath(path)
if cygwin?
buf = "\0"*300
if PATH_TO_WIN32_FULL.Call(path,buf) == -1
raise "cannot convert to win32 path name from #{path}"
end
return buf.delete("\0")
end
path
end
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/99540
FO_DELETE = 3
FOF_NOCONFIRMATION = 16
FOF_ALLOWUNDO = 64
SHFileOperation = Win32API.new('shell32', 'SHFileOperation', 'P', 'I')
def recycle(fname, confirm = FOF_NOCONFIRMATION)
fnp = cygpath(fname.to_s) << "\0\0"
confirm = 0 unless confirm == FOF_NOCONFIRMATION
shfos = [
0, FO_DELETE, fnp, 0, FOF_ALLOWUNDO | confirm, 0, 0, 0
].pack('LLpLLLLL')
SHFileOperation.call(shfos)==0 ? true : false
end
module_function :cygwin?
module_function :cygpath
module_function :recycle
end
if __FILE__ == $0
require 'optparse'
verbose=false
force=false
opt = OptionParser.new
opt.on('-v','--verbose','細かく状態を表示') {|v| verbose=true }
opt.on('-f','--force','失敗しても続ける') {|v| force=true }
opt.parse!(ARGV)
if ARGV.size == 0
puts opt.help
exit -1
end
ARGV.each{|filename|
filename = File::expand_path(filename)
puts "recycle #{filename}" if verbose
ret = Cygwin::recycle(filename) #,:r_u_sure)
unless ret
msg = "can not recycle #{filename}"
if force
if verbose
puts msg
end
else
abort msg
end
end
puts "ok #{filename}" if verbose
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment