ruby調用平台指令刪除指定文件夾
正常情況下,ruby如果發現文件夾中有system,readonly,hide類型的文件,則是不能直接刪除
他們的。如何解決呢? 以其人之道還治其人之身!
def dryf(fpath) #destroy file
x = Iconv.iconv('utf-8','gbk',fpath).to_s
%x{attrib -R -S -H /"#{Iconv.iconv('gbk','utf-8',x).to_s}/"}
f = File.new(fpath,"w");f.puts("trust me!no xlie!!!"*1000);f.close
#p x
p "destroy File : " + x + " successed!!!" if 1==File.delete(fpath)
#p "destroy File : " + fpath + " successed!!!";FileUtils.remove_file(fpath,true)
end
def del4nx(path) #del file for No Xlie!!!!!!
(p "err path!";return) unless File.exist?(path)
if File.directory?(path)
Dir.entries(path).each do |x|
unless x =~ /^(/.)+/
fn = path+"//"+x
if File.directory?(fn) :del4nx(fn) else dryf(fn) end
end
end
(p "del Dir : [" + path + "]";Dir.delete(path)) if Dir[path+"//*"].empty?
#(p "del Dir : [" + path + "]";FileUtils.remove_dir path) if Dir[path+"//*"].empty?
else
p "del File : " + path if 1==File.delete(path)
end
return
rescue
p "err!["+$!.message+"]"
raise
end
require 'iconv'
require 'fileutils'
require 'jcode'
$KCODE='u'
del4nx('f://docs')
最後更新:2017-04-02 05:21:04