[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: fastest way of looping through directories

Ryan McGovern

7/10/2006 2:13:00 PM

To do a deep listing i do a recursive algorithm with Dir.foreach. Here
is some code that counts up the sizes you could change the return values
so that it returns a list of files and directories. this also stops if
the depth is >24 this helps to stop if it catches a hardlink that makes
it loop on it's self.

def deepsize(path,depth)
if(depth>24)
return 0
end
if(!File.exists?(path))
return 0
end
d=Dir.new(path)
cont=true
size=0
while(cont)
sub=d.read
if(sub==nil)
break
end

if(sub=="."||sub=="..")

next
end
st=File.lstat(path+"/"+sub)
if(st.file?)
size+=st.size

else

if(!st.symlink?&&st.directory?)
size+=deepsize(path+"/"+sub,depth+1)

end
end
end
return size
end

Thomas Coopman wrote:
> Hi,
>
> I'm looking for the fastest way for looping through directories.
>
> at the moment i'm using Find but when I test this, the first time it runs
> really slow (like 20 seconds) the second time it runs much much quicker
> (less than a second).
> The problem is that I don't need to run this twice but only once. Is
> this
> because I use the unit tests that it runs slow or is it something else.
>
> I only need the path of all the files in the directory + subdirectories.
> What would be the fastest for this?
> using Dir.foreach or Find or something else
>
> Thanks for the help
>