# Return actual files only not directories, using Pathname

**URL:** <https://www.ruby-forum.com/t/return-actual-files-only-not-directories-using-pathname/159510>\
**Category:** Ruby\
**Created:** [February 5, 2009, 7:51am UTC](https://www.ruby-forum.com/t/return-actual-files-only-not-directories-using-pathname/159510 "2009-02-05T07:51:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [February 5, 2009, 7:51am UTC](https://www.ruby-forum.com/t/return-actual-files-only-not-directories-using-pathname/159510/1 "2009-02-05T07:51:54Z")

</div>

I’m very new to Ruby, but I’m learning pretty quickly …What i’m trying  
to  
do is fairly simple…

I’d like to use Pathname, to return only actual filenames in the  
array…  
For instance , using FileList… You can just do this:

FileList["_._"]

And it will return a string array with the filenames without  
directories…

But I rather like Pathname … Since I can use it like this:

a = Pathname("/Path")  
b = a.children

puts b[1] #==\> “/Path/file1.txt”

b[1].read #==\> “This is data in a file named file1.txt”  
puts b[2] #==\> “/Path/file2.txt”  
…

Does anyone have any tips on how I could use Pathname to return files  
only,  
and not directories?

Thanks,

Brian

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [February 5, 2009, 8:58am UTC](https://www.ruby-forum.com/t/return-actual-files-only-not-directories-using-pathname/159510/2 "2009-02-05T08:58:54Z")

</div>

Brian W. wrote:

> I’d like to use Pathname, to return only actual filenames in the  
> array…  
> For instance , using FileList… You can just do this:
> 
> FileList["_._"]

FileList? I can’t find anything in the ruby docs about FileList.  
google doesn’t help either.

> Does anyone have any tips on how I could use Pathname to return files  
> only,  
> and not directories?

Just separate out the files after you retrieve all the file names:

require “pathname”

pn\_obj = Pathname.new("./")  
file\_objs = pn\_obj.children

dirs = []  
files = []

file\_objs.each do |obj|  
if obj.directory?  
dirs \<\< obj  
else  
files \<\< obj  
end  
end

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [February 5, 2009, 9:01am UTC](https://www.ruby-forum.com/t/return-actual-files-only-not-directories-using-pathname/159510/3 "2009-02-05T09:01:04Z")

</div>

On Thu, Feb 5, 2009 at 1:50 AM, Brian W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Does anyone have any tips on how I could use Pathname to return files only,  
> and not directories?

Use Pathname.glob

> > require “pathname”  
> > =\> true  
> > Pathname.glob(“lib/\*\*/\*.rb”)  
> > =\> [#Pathname:lib/prawn/compatibility.rb,  
> > #Pathname:lib/prawn/document/annotations.rb,  
> > #Pathname:lib/prawn/document/bounding\_box.rb,  
> > #Pathname:lib/prawn/document/destinations.rb,  
> > #Pathname:lib/prawn/document/internals.rb,  
> > #Pathname:lib/prawn/document/page\_geometry.rb,  
> > #Pathname:lib/prawn/document/span.rb,  
> > #Pathname:lib/prawn/document/text/box.rb,  
> > #Pathname:lib/prawn/document/text/wrapping.rb,  
> > #Pathname:lib/prawn/document/text.rb,  
> > #Pathname:lib/prawn/document.rb, #Pathname:lib/prawn/encoding.rb,  
> > #Pathname:lib/prawn/errors.rb, #Pathname:lib/prawn/font/afm.rb,  
> > #Pathname:lib/prawn/font/dfont.rb,  
> > #Pathname:lib/prawn/font/ttf.rb, #Pathname:lib/prawn/font.rb,  
> > #Pathname:lib/prawn/graphics/color.rb,  
> > #Pathname:lib/prawn/graphics.rb,  
> > #Pathname:lib/prawn/images/jpg.rb,  
> > #Pathname:lib/prawn/images/png.rb, #Pathname:lib/prawn/images.rb,  
> > #Pathname:lib/prawn/literal\_string.rb,  
> > #Pathname:lib/prawn/measurement\_extensions.rb,  
> > #Pathname:lib/prawn/measurements.rb,  
> > #Pathname:lib/prawn/name\_tree.rb,  
> > #Pathname:lib/prawn/pdf\_object.rb,  
> > #Pathname:lib/prawn/reference.rb, #Pathname:lib/prawn.rb]

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [February 5, 2009, 10:52am UTC](https://www.ruby-forum.com/t/return-actual-files-only-not-directories-using-pathname/159510/4 "2009-02-05T10:52:20Z")

</div>

Gregory B. wrote:

> On Thu, Feb 5, 2009 at 1:50 AM, Brian W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Does anyone have any tips on how I could use Pathname to return files only,  
> > and not directories?
> 
> Use Pathname.glob
> 
> > > require “pathname”  
> > > =\> true  
> > > Pathname.glob(“lib/\*\*/\*.rb”)

You will have to be cautious with this one though, since a directory  
name may contain dots such as ‘project.rb’. I guess it’s highly unusual  
but I have seen this before …

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [February 5, 2009, 9:06am UTC](https://www.ruby-forum.com/t/return-actual-files-only-not-directories-using-pathname/159510/5 "2009-02-05T09:06:56Z")

</div>

From: Brian W. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I’d like to use Pathname,

Pathname is a very nice façade for a lot of file things 😉

# to return only actual filenames in the array…

# But I rather like Pathname … Since I can use it like this:

# a = Pathname(“/Path”)

# b = a.children

# puts b[1] #==\> “/Path/file1.txt”

# b[1].read #==\> “This is data in a file named file1.txt”

# puts b[2] #==\> “/Path/file2.txt”

# …

try passing a boolean arg to children, eg

b = a.children(false)

# Does anyone have any tips on how I could use Pathname to

# return files only, and not directories?

\*nixism, they’re simply all files, you’ll have ask more.

try #file? or #directory?, eg

b[1].file?

b[1].directory?

warning: be careful when mixing result of #children(false) with other  
pathname methods.

on your case, to get only the files (non-dir ie), i’d probably just do,

b = a.children.select do |path|  
path.file?  
end

and to get a list of the names (as strings ie)

b.map do |path|  
path.basename.to\_s  
end

and if you want to get a separate list of files and dirs, try partition,  
eg

files, dirs = a.children.partition do |path|  
path.file?  
end

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [February 5, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/return-actual-files-only-not-directories-using-pathname/159510/6 "2009-02-05T16:05:06Z")

</div>

On Feb 5, 2009, at 1:58 AM, 7stud – wrote:

> google doesn’t help either.  
> It’s part of Rake.

James Edward G. II
