Return actual files only not directories, using Pathname


#1

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


#2

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


#3

On Thu, Feb 5, 2009 at 1:50 AM, Brian W. 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]


#4

Gregory B. wrote:

On Thu, Feb 5, 2009 at 1:50 AM, Brian W. 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 …


#5

From: Brian W. [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 :wink:

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


#6

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

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

James Edward G. II