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

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
.

Filename1.exe
Filename2.html
Filename3.txt

On Monday 20 July 2009 03:05:18 pm Mmcolli00 Mom wrote:

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

Thanks David, I try them out now! -Misty

On Mon, Jul 20, 2009 at 4:19 PM, David M.[email protected]
wrote:

I’d do this:I

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

I would just do something like this:

Dir.entries(dir).reject{|entry| entry =~ /^.{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 == “.” || entry == “…”}

That’s really just semantics, though.

Alex

Alex wrote:

On Mon, Jul 20, 2009 at 4:19 PM, David M.[email protected]
wrote:

I’d do this:I

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

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

On Jul 20, 2009, at 6:48 PM, 7stud – wrote:

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

Unless you want it to:
Dir.glob("") #=> [“config.h”, “main.rb”]
Dir.glob("
", File::FNM_DOTMATCH) #=> [".", “…”, “config.h”,
“main.rb”]

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

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

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”)

Mmcolli00 Mom wrote:

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

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”)

disregard this, replied to wrong post - sorry :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs