# How do I scan a directory tree using a Rake task?

**URL:** <https://www.ruby-forum.com/t/how-do-i-scan-a-directory-tree-using-a-rake-task/133649>\
**Category:** Rails\
**Created:** [April 2, 2008, 10:46am UTC](https://www.ruby-forum.com/t/how-do-i-scan-a-directory-tree-using-a-rake-task/133649 "2008-04-02T10:46:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tonkatsufan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a4c791/32.png) [@Tonkatsufan](https://www.ruby-forum.com/u/Tonkatsufan)\
**Post date:** [April 2, 2008, 10:46am UTC](https://www.ruby-forum.com/t/how-do-i-scan-a-directory-tree-using-a-rake-task/133649/1 "2008-04-02T10:46:45Z")

</div>

Hi everybody,

in my application, I want to offer file downloads on a per-user basis.  
That means, a user has to authenticate and gets assigned a user\_id.

Now, in my filesystem, I have a directory for each user\_id (above  
webroot, of course) which contains subdirectories and files. What I  
want to do is offer the files under each user’s directory for  
download. These files are uploaded or updated at random intervals with  
rsync from another server. I have figured out how to secure the  
download part with Apache and xsendfile, and it works fine.

But, what I need help with is, whenever there have been uploaded new  
files, I want to have a rake task that scans the directories for all  
the users and writes each file with it’s path into a db table. I also  
would like to get access to all the dir names and filenames found, so  
I can split them up and use them as keys etc.

I searched the Rails groups but haven’t really found any pointers how  
this could be done.

Thanks for any help you can give me.

---

<div class="post-metadata">

**Author:** ![Tonkatsufan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a4c791/32.png) [@Tonkatsufan](https://www.ruby-forum.com/u/Tonkatsufan)\
**Post date:** [April 12, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/how-do-i-scan-a-directory-tree-using-a-rake-task/133649/2 "2008-04-12T23:47:31Z")

</div>

This is more a ruby question than a rails question. Maybe try in the  
Ruby forum.

I assume you’ve already investigated the built in Dir#glob method
