# Folder delete script

**URL:** <https://www.ruby-forum.com/t/folder-delete-script/90890>\
**Category:** Ruby\
**Created:** [April 2, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/folder-delete-script/90890 "2007-04-02T19:51:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gus\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6bbea6/32.png) [@Gus\_H](https://www.ruby-forum.com/u/Gus_H)\
**Post date:** [April 2, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/folder-delete-script/90890/1 "2007-04-02T19:51:04Z")

</div>

Hi. I am a rubyn00b. I need help. I’m trying to write a script that will  
delete specific sub folders in a particular folder. I would like to  
delete  
all folders that begin with lets say the letter “T” that are two weeks  
old.  
It would probably work like the deltree command in dos (deltree T\*.\*  
/s).  
How would I write that in ruby? Is it also possible to use wildcards in  
ruby? If so how? Any help would be greatly appreciated. TY!

---

<div class="post-metadata">

**Author:** ![Gus\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6bbea6/32.png) [@Gus\_H](https://www.ruby-forum.com/u/Gus_H)\
**Post date:** [April 2, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/folder-delete-script/90890/2 "2007-04-02T20:10:05Z")

</div>

On 4/2/07, Gus H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi. I am a rubyn00b. I need help. I’m trying to write a script that will  
> delete specific sub folders in a particular folder. I would like to delete  
> all folders that begin with lets say the letter “T” that are two weeks old.  
> It would probably work like the deltree command in dos (deltree T\*.\* /s).  
> How would I write that in ruby? Is it also possible to use wildcards in  
> ruby? If so how? Any help would be greatly appreciated. TY!

The Dir.glob command will help you find folders that start with a  
particular letter. The File.stat command will help you determine the  
creation / modification times of those folders and whether or not they  
are folders at all.

The documentation for these items can be found on the Ruby-doc website.

[http://www.ruby-doc.org/core/](http://www.ruby-doc.org/core/)

Have fun learning Ruby! Ruby has taught me more about programming  
than any other language out there. The community is simply wonderful,  
too.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![Gus\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6bbea6/32.png) [@Gus\_H](https://www.ruby-forum.com/u/Gus_H)\
**Post date:** [April 2, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/folder-delete-script/90890/3 "2007-04-02T20:18:06Z")

</div>

On Tue, 3 Apr 2007, Gus H. wrote:

> Hi. I am a rubyn00b. I need help. I’m trying to write a script that will  
> delete specific sub folders in a particular folder. I would like to delete  
> all folders that begin with lets say the letter “T” that are two weeks old.  
> It would probably work like the deltree command in dos (deltree T\*.\* /s).  
> How would I write that in ruby? Is it also possible to use wildcards in  
> ruby? If so how? Any help would be greatly appreciated. TY!

harp:~ \> cat a.rb  
require ‘fileutils’

pattern = ‘T\*.\*’

Dir.glob(’ **/** ’) do |entry|  
next unless test ?d, entry  
FileUtils.rm\_rf entry if File.fnmatch pattern, entry  
end

-a
