# Delete files in a directory

**URL:** <https://www.ruby-forum.com/t/delete-files-in-a-directory/119123>\
**Category:** Ruby\
**Created:** [November 3, 2007, 11:39am UTC](https://www.ruby-forum.com/t/delete-files-in-a-directory/119123 "2007-11-03T11:39:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oderas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oderas](https://www.ruby-forum.com/u/oderas)\
**Post date:** [November 3, 2007, 11:39am UTC](https://www.ruby-forum.com/t/delete-files-in-a-directory/119123/1 "2007-11-03T11:39:02Z")

</div>

Hello, this is my first post here, so first i would like to thanks all  
the people who works for this site.

Well, i’m trying to set up a little script that should delete all the  
files in a given drectory; the system should work like this:  
-I have the name and the path of file = “/Users…file1”  
-from file, i should get the directory (I’ve used File.dirname(file))  
-from that directory, i should delete all files and sub directories  
except from the given file (in this case, file1)

I’m making confusion when i try to iterare on the files and irectory in  
that folder, and i’m scared to make test of deleting files in the system

Oscar

---

<div class="post-metadata">

**Author:** ![oderas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oderas](https://www.ruby-forum.com/u/oderas)\
**Post date:** [November 3, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/delete-files-in-a-directory/119123/2 "2007-11-03T14:26:18Z")

</div>

Alle sabato 3 novembre 2007, Oscar Del ben ha scritto:

> I’m making confusion when i try to iterare on the files and irectory in  
> that folder, and i’m scared to make test of deleting files in the system
> 
> Oscar

If I understand correctly what you need to do, this should work.

require ‘fileutils’

file\_name = File.basename(file\_path)  
dir = File.dirname(file\_path)  
Dir.foreach(dir) do |f|  
if f == file\_name or f == ‘.’ or f == ‘…’ then next  
elsif File.directory?(f) then FileUtils.rm\_rf(f)  
else FileUtils.rm( f )  
end  
end

To test the script, you can replace FileUtils.rm\_rf(f) and  
FileUtils.rm(f) respectively with

FileUtils.rm\_rf( f, :noop =\> true, :verbose =\> true)

and

FileUtils.rm( f, :noop =\> true, :verbose =\> true)

With those arguments, FileUtils.rm and FileUtils.rm\_rf don’t actually  
delete the files (or directories), but print the operation that would be  
performed on screen, so you can check whether everything works. For more  
information on this, look at the ri documentation for FileUtils and  
FileUtils.rm.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![oderas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oderas](https://www.ruby-forum.com/u/oderas)\
**Post date:** [November 4, 2007, 10:39am UTC](https://www.ruby-forum.com/t/delete-files-in-a-directory/119123/3 "2007-11-04T10:39:26Z")

</div>

Thank you Stefano, it work good for me!

---

<div class="post-metadata">

**Author:** ![oderas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oderas](https://www.ruby-forum.com/u/oderas)\
**Post date:** [November 5, 2007, 9:01am UTC](https://www.ruby-forum.com/t/delete-files-in-a-directory/119123/4 "2007-11-05T09:01:28Z")

</div>

require ‘fileutils’

def delete(filename)  
Dir["#{File.dirname(filename)}/\*"].each do |file|  
next if File.basename(file) == File.basename(filename)  
FileUtils.rm\_rf file, :noop =\> true, :verbose =\> true  
end  
end

USAGE:

delete ‘path/to/file1’

Bye.  
Andrea

Il giorno sab, 03/11/2007 alle 19.39 +0900, Oscar Del ben ha scritto:

---

<div class="post-metadata">

**Author:** ![oderas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8797f3/32.png) [@oderas](https://www.ruby-forum.com/u/oderas)\
**Post date:** [November 5, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/delete-files-in-a-directory/119123/5 "2007-11-05T14:44:11Z")

</div>

Thank you!
