Return actual files only not directories, using Pathname..

B

Brian Wallace

[Note: parts of this message were removed to make it a legal post.]

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
 
7

7stud --

Brian said:
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
 
G

Gregory Brown

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

Use Pathname.glob
=> [#<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>]
 
P

Peña, Botp

From: Brian Wallace [mailto:[email protected]]=20
# I'd like to use Pathname,=20

Pathname is a very nice fa=E7ade 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 =3D Pathname("/Path")
# b =3D a.children
# puts b[1] #=3D=3D> "/Path/file1.txt"
# b[1].read #=3D=3D> "This is data in a file named file1.txt"
# puts b[2] #=3D=3D> "/Path/file2.txt"
# .....

try passing a boolean arg to children, eg

b =3D a.children(false)



# Does anyone have any tips on how I could use Pathname to=20
# 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 =3D a.children.select do |path|
path.file?
end

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

b.map do |path|=20
path.basename.to_s=20
end


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

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

Sascha Abel

Gregory said:
Use Pathname.glob

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 ...
 
J

James Gray

Brian said:
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.

It's part of Rake.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top