# Creating directory structures

**URL:** <https://www.ruby-forum.com/t/creating-directory-structures/155540>\
**Category:** Ruby\
**Created:** [December 13, 2008, 6:03am UTC](https://www.ruby-forum.com/t/creating-directory-structures/155540 "2008-12-13T06:03:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 13, 2008, 6:03am UTC](https://www.ruby-forum.com/t/creating-directory-structures/155540/1 "2008-12-13T06:03:59Z")

</div>

I’m having a bit of a difficult time coming up with a solid way handle  
this problem. I’ll explain:

I have an array containing a list of file paths like the following.

[“foo/bar/blah.rb”,“foo/bar/stuff.rb”,"foo/bar/example/thing.rb,“foo/ham/example”]

I need to come up with a way to create a directory structure using the  
paths supplied in the array. The “base” directory is unimportant, but  
the structure of the rest is. Using the above as an example, I would  
need to create the following directories.

foo/bar  
foo/bar/example  
foo/ham  
foo/ham/example

I also need to check whether or not the directory exists so that it  
makes no attempt to create a directory that is already there. Any ideas?

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 13, 2008, 6:21am UTC](https://www.ruby-forum.com/t/creating-directory-structures/155540/2 "2008-12-13T06:21:34Z")

</div>

On Dec 12, 2008, at 10:56 PM, Ch Ba wrote:

> the structure of the rest is. Using the above as an example, I would  
> need to create the following directories.
> 
> foo/bar  
> foo/bar/example  
> foo/ham  
> foo/ham/example
> 
> I also need to check whether or not the directory exists so that it  
> makes no attempt to create a directory that is already there. Any  
> ideas?

FileUtils.makedirs( files.map{ |f| f.dirname }.uniq.reject{ |d|  
File::exist?(d) } )

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 13, 2008, 6:37am UTC](https://www.ruby-forum.com/t/creating-directory-structures/155540/3 "2008-12-13T06:37:44Z")

</div>

Matthew M. wrote:

> FileUtils.makedirs( files.map{ |f| f.dirname }.uniq.reject{ |d|  
> File::exist?(d) } )

I’ve got to be honest, I was looking for more of a  
discussion/explanation than code itself. I greatly appreciate your  
prompt response and you putting it together but my application is a bit  
more advanced than I led on and what I really need is to better  
understand how to do it =)

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 13, 2008, 10:53am UTC](https://www.ruby-forum.com/t/creating-directory-structures/155540/4 "2008-12-13T10:53:03Z")

</div>

Ch Ba wrote:

> I’m having a bit of a difficult time coming up with a solid way handle  
> this problem. I’ll explain:
> 
> I have an array containing a list of file paths like the following.
> 
> [“foo/bar/blah.rb”,“foo/bar/stuff.rb”,"foo/bar/example/thing.rb,“foo/ham/example”]
> 
> I need to come up with a way to create a directory structure using the  
> paths supplied in the array. The “base” directory is unimportant, but  
> the structure of the rest is. Using the above as an example, I would  
> need to create the following directories.
> 
> foo/bar  
> foo/bar/example  
> foo/ham  
> foo/ham/example
> 
> I also need to check whether or not the directory exists so that it  
> makes no attempt to create a directory that is already there. Any ideas?

Maybe the whole array idea is just bad altogether. I’ll start from the  
top and explain what I’m trying to do.

Starting with a base directory, we will say “foo”, I need to iterate  
through all subdirectories, gathering only files that contain certain  
characters in their name. That part was easy, and it is returning the  
array like I mentioned previously. Should I be doing it differently?  
After gathering the files, a new file is created for each file  
gathered, and placed in a seperate location, with a new base directory.  
The structure of the subdirectories must be maintained however. I’m  
having an immensly difficult time getting this to work. I’ve tried  
several different options, what do you guys think the best way to handle  
this task would be?

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 13, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/creating-directory-structures/155540/5 "2008-12-13T15:45:00Z")

</div>

> Starting with a base directory, we will say “foo”, I need to iterate  
> through all subdirectories, gathering only files that contain certain  
> characters in their name. That part was easy, and it is returning the  
> array like I mentioned previously. Should I be doing it differently?

That seems like a quite reasonable way to do it.

> After gathering the files, a new file is created for each file  
> gathered, and placed in a seperate location, with a new base  
> directory.  
> The structure of the subdirectories must be maintained however. I’m  
> having an immensly difficult time getting this to work. I’ve tried  
> several different options, what do you guys think the best way to  
> handle  
> this task would be?

Once you have an array of the source files, you should be able to  
create a list of the potential new files by replacing part of the  
source paths (using String#gsub).

After that, enumerate your files, creating the directory (using  
File.dirname) and then creating the file.

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 13, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/creating-directory-structures/155540/6 "2008-12-13T18:13:24Z")

</div>

> Once you have an array of the source files, you should be able to  
> create a list of the potential new files by replacing part of the  
> source paths (using String#gsub).
> 
> After that, enumerate your files, creating the directory (using  
> File.dirname) and then creating the file.

Excellent. That’s what I’m looking for, I will follow up with my  
results.

---

<div class="post-metadata">

**Author:** ![navouri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@navouri](https://www.ruby-forum.com/u/navouri)\
**Post date:** [December 14, 2008, 11:05am UTC](https://www.ruby-forum.com/t/creating-directory-structures/155540/7 "2008-12-14T11:05:38Z")

</div>

On 13.12.2008 18:09, Ch Ba wrote:

> > Once you have an array of the source files, you should be able to  
> > create a list of the potential new files by replacing part of the  
> > source paths (using String#gsub).
> > 
> > After that, enumerate your files, creating the directory (using  
> > File.dirname) and then creating the file.
> 
> Excellent. That’s what I’m looking for, I will follow up with my  
> results.

There is another option that does not need gsub: use Dir[] with a  
directory wildcard and then just prepend source and target dir. Also,  
you can use FileUtils.mkdir\_p which handles intermediate as well as  
existing directories gracefully.

Cheers

robert

# ============= SPOILER BELOW =======================

require ‘fileutils’

Dir.chdir source\_dir do  
Dir[’\*\*/_special_characters\*’]  
end.each do |file|  
from = File.join(source\_dir, file)

if must\_transform? from  
to = File.join(target\_dir, file)  
FileUtils.mkdir\_p(File.dirname(to))  
transform(from, to)  
end  
end
