Dir.entries showing . and .. , how come?

M

Mmcolli00 Mom

Why is it that there are '.' and '..' in my output? I want to create a
list of all the files in this directory without showing the . and .. at
the top of the list. What do you do for this? Thanks, M

#snippet
myfolder = "c:/myfolder"

Dir.entries(myfolder).each do |filename|
puts filename
end
#end snippet

#Shows this as output
 
D

David Masover

Why is it that there are '.' and '..' in my output?

Those are hidden directories on Unix, pointing to the current and parent
directory. I'm not sure why they're on Windows, too...
I want to create a
list of all the files in this directory without showing the . and .. at
the top of the list. What do you do for this?

I'd do this:

#snippet
myfolder = "c:/myfolder"

Dir.entries(myfolder).each do |filename|
puts filename unless filename =~ /^\.\.?$/
end
#end snippet

You could do it other ways, too. I'm not sure which is faster.

Of course, this might still not do what you want -- it's going to include
folders, too. If you just want files, you could do something like this:

Dir.entries(myfolder).each do |filename|
puts filename if File.file?(filename)
end
 
A

Alex

Those are hidden directories on Unix, pointing to the current and parent
directory. I'm not sure why they're on Windows, too...


I'd do this:I

#snippet
myfolder =3D "c:/myfolder"

Dir.entries(myfolder).each do |filename|
=A0puts filename unless filename =3D~ /^\.\.?$/
end
#end snippet

You could do it other ways, too. I'm not sure which is faster.

Of course, this might still not do what you want -- it's going to include
folders, too. If you just want files, you could do something like this:

Dir.entries(myfolder).each do |filename|
=A0puts filename if File.file?(filename)
end


I would just do something like this:

Dir.entries(dir).reject{|entry| entry =3D~ /^\.{1,2}$/}

But my regex-fu may be weak. Since it's only 2 possibilities, it might
be simpler to just do this:

Dir.entries(dir).reject{|entry| entry =3D=3D "." || entry =3D=3D ".."}


That's really just semantics, though.



Alex
 
7

7stud --

Alex said:
I would just do something like this:

Dir.entries(dir).reject{|entry| entry =~ /^\.{1,2}$/}


On unix, Dir.glob doesn't return hidden files(like Dir.entries does):


dirname = "whatever"

Dir.glob("#{dirname}/*").each do |fname|
puts fname
end
 
R

Rob Biedenharn

On unix, Dir.glob doesn't return hidden files(like Dir.entries does):
dirname =3D "whatever"

Dir.glob("#{dirname}/*").each do |fname|
puts fname
end
--=20

Unless you want it to:
Dir.glob("*") #=3D> ["config.h", "main.rb"]
Dir.glob("*", File::FNM_DOTMATCH) #=3D> [".", "..", "config.h", =20
"main.rb"]

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
M

Mmcolli00 Mom

dirname = "whatever"

Dir.glob("#{dirname}/*").each do |fname|
puts fname
end

this returned 1 nil since first file in the directory was not the file
for the condition. (ex. "AUR")
 
M

Mmcolli00 Mom

Mmcolli00 said:
this returned 1 nil since first file in the directory was not the file
for the condition. (ex. "AUR")


disregard this, replied to wrong post - sorry :)
 

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
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top