From: Darken Date: 2006-06-23T00:03:18+09:00 Subject: Re: disk_free_space Thank you... parsing df output is working fine... anyway,it will be nice to have more... ruby.. solution. > Darken wrote: > >> I need an equivalent of $subj php function in ruby... I have a directory >> and I need to find out how much I can save there... Only idea I have is >> to find mountpoint and then parse /proc, but it's very ugly solution :-) >> > > You can do this simply if not truly portably like so: > > class << self > define_method :disk_free_space, &begin > require 'Win32API' > df = Win32API.new("kernel32", "GetDiskFreeSpaceEx", "pppp", "i") > proc do |dir| > df.call(dir, ret=" ", " ", " ") > ret.unpack("L")[0] > end > rescue LoadError > proc do |dir| > `df -k #{dir}`[/\S+\s+\S+\s+\S+\s+(\d+)/, 1].to_i * 1024 > end > end > end > > Obviously if your `df -k foo` gives you output in a different format, > you'll need to change the regular expression. (The way I've written it, > it's looking for available space in 1k blocks in the fourth column.) > > Cheers, > Dave > > >