# Newbie: Recursive deletion of files and dir

**URL:** <https://rubytalk.org/t/newbie-recursive-deletion-of-files-and-dir/24240>\
**Category:** ruby-talk\
**Created:** [22 January 2006 21:34 UTC](https://rubytalk.org/t/newbie-recursive-deletion-of-files-and-dir/24240 "2006-01-22T21:34:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![FredrikC](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@FredrikC](https://rubytalk.org/u/FredrikC)\
**Post date:** [22 January 2006 21:34 UTC](https://rubytalk.org/t/newbie-recursive-deletion-of-files-and-dir/24240/1 "2006-01-22T21:34:25Z")

</div>

Hi!  
I'm designing (newbie level) a script to clean up empty directories and  
old files recursivly. Something is wrong..works fine when not actually  
deleting anything, but when I start removing files and directories it  
ends prematurly.. any heklp available..?

def thisDir(dirname,f)  
&nbsp;&nbsp;Dir.chdir(dirname)  
&nbsp;&nbsp;currDir=Dir.getwd  
&nbsp;&nbsp;nf=0  
&nbsp;&nbsp;nd=0  
&nbsp;&nbsp;fs=0.0

&nbsp;&nbsp;if Dir.entries(currDir).size\>2 then  
&nbsp;&nbsp;&nbsp;&nbsp;Dir.foreach(currDir) {|x|  
&nbsp;&nbsp;&nbsp;&nbsp;if FileTest.file?(x) && (Time.now-File.mtime(x))/(60\*60\*24)\>15 then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.puts("deleting file"+x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nf=nf+1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs=fs+File.stat(x).size  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;if FileTest.directory?(x) && x!="." && x!=".." then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lV=thisDir(x,f)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nf=nf+lV[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nd=nd+lV[1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs=fs+lV[2]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;Dir.chdir("..")  
&nbsp;&nbsp;end  
&nbsp;&nbsp;if Dir.entries(currDir).size\<=2 then  
&nbsp;&nbsp;&nbsp;&nbsp;Dir.chdir("..")  
&nbsp;&nbsp;&nbsp;&nbsp;f.puts("deleteting this directory:"+currDir)  
&nbsp;&nbsp;&nbsp;&nbsp;nd=nd+1  
&nbsp;&nbsp;&nbsp;&nbsp;#Dir.delete(currDir)  
&nbsp;&nbsp;end

&nbsp;&nbsp;#Dir.chdir("..")  
return [nf,nd,fs]  
end  
outFileName="cleaned-"+Date.today.to\_s+".log"  
f=File.new(outFileName,"w")  
values=thisDir("./",f)  
puts "Deleted "+values[0].to\_s+" files"  
puts "Deleted "+values[1].to\_s+" directories"  
puts "Total size deleted:"+(values[2]/(1024)).to\_s+" kB"

Any advice is appriciated

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Eero\_Saynatkari1](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eero\_Saynatkari1](https://rubytalk.org/u/Eero_Saynatkari1)\
**Post date:** [22 January 2006 21:52 UTC](https://rubytalk.org/t/newbie-recursive-deletion-of-files-and-dir/24240/2 "2006-01-22T21:52:32Z")

</div>

FredrikC wrote:

> Hi!  
> I'm designing (newbie level) a script to clean up empty directories and  
> old files recursivly. Something is wrong..works fine when not actually  
> deleting anything, but when I start removing files and directories it  
> ends prematurly.. any heklp available..?
> 
> def thisDir(dirname,f)  
> &nbsp;&nbsp;Dir.chdir(dirname)  
> &nbsp;&nbsp;currDir=Dir.getwd  
> &nbsp;&nbsp;nf=0  
> &nbsp;&nbsp;nd=0  
> &nbsp;&nbsp;fs=0.0
> 
> &nbsp;&nbsp;if Dir.entries(currDir).size\>2 then  
> &nbsp;&nbsp;&nbsp;&nbsp;Dir.foreach(currDir) {|x|  
> &nbsp;&nbsp;&nbsp;&nbsp;if FileTest.file?(x) && (Time.now-File.mtime(x))/(60\*60\*24)\>15 then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.puts("deleting file"+x)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nf=nf+1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs=fs+File.stat(x).size  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;if FileTest.directory?(x) && x!="." && x!=".." then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lV=thisDir(x,f)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nf=nf+lV[0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nd=nd+lV[1]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs=fs+lV[2]  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;Dir.chdir("..")  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;if Dir.entries(currDir).size\<=2 then  
> &nbsp;&nbsp;&nbsp;&nbsp;Dir.chdir("..")  
> &nbsp;&nbsp;&nbsp;&nbsp;f.puts("deleteting this directory:"+currDir)  
> &nbsp;&nbsp;&nbsp;&nbsp;nd=nd+1  
> &nbsp;&nbsp;&nbsp;&nbsp;#Dir.delete(currDir)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;#Dir.chdir("..")  
> return [nf,nd,fs]  
> end  
> outFileName="cleaned-"+Date.today.to\_s+".log"  
> f=File.new(outFileName,"w")  
> values=thisDir("./",f)  
> puts "Deleted "+values[0].to\_s+" files"  
> puts "Deleted "+values[1].to\_s+" directories"  
> puts "Total size deleted:"+(values[2]/(1024)).to\_s+" kB"
> 
> Any advice is appriciated

I did not read this code yet but take a look at ri FileUtils,  
it has useful methods including two that might be handy here:

&nbsp;&nbsp;FileUtils.rm\_rf  
&nbsp;&nbsp;FileUtils.mkdir\_p

E

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
