Discussion:
Trying to identify directories
leam hall
2018-07-30 11:46:56 UTC
Permalink
Trying to figure out why this doesn't put just the directory names.
There are directories in options[:source] and if I start an irb
session in that directory, pick a directory name like "docs", and do:
File.directory?('docs')
it returns true.

Besides a brain, a clue. and a life, what am I missing?

Thanks!

Leam

###
Dir.entries(options[:source]).each {|node|
if File.directory?(node)
puts " node is: #{node}."
end
}

# output, even though there are directories present:
node is: ..
node is: ...

# If I remove the "File.directory?() it puts all the files and directories.

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Gonzalo Garramuño
2018-07-30 12:26:42 UTC
Permalink
Post by leam hall
Trying to figure out why this doesn't put just the directory names.
There are directories in options[:source] and if I start an irb
File.directory?('docs')
it returns true.
Besides a brain, a clue. and a life, what am I missing?
You are missing the full path to the directory or Dir.chdir to the
directory you call the script from
--
Gonzalo Garramuño


Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
leam hall
2018-07-30 22:39:02 UTC
Permalink
You are missing the full path to the directory or Dir.chdir to the directory
you call the script from
--
Gonzalo Garramuño
And this was it. I need to spend some time figuring out how to handle
this with recursion, but it's the root cause.

Thanks!

Leam

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bi
Leam Hall
2018-07-31 00:57:33 UTC
Permalink
Post by leam hall
You are missing the full path to the directory or Dir.chdir to the directory
you call the script from
And this was it. I need to spend some time figuring out how to handle
this with recursion, but it's the root cause.
Updated, and full code. Still working on the final product, but this is
a big step forward. I'm learning to use Ruby on files and file-systems.
This task is to find all the '.git' directories so I can grab the
repository URLs and clone them into a new directory.

Leam

####

# ruby source_dest_dir -s /home/me/lang/git

require 'optparse'
git_dirs = []
options = Hash.new{}

option_parser = OptionParser.new {|opts|
opts.on('-s DIR', '--source', "Source") {|s|
options[:source] = s
}
opts.on('-d DIR', '--dest', "Destination") {|d|
options[:dest] = d
}
}
option_parser.parse!

begin
options[:dest] = Dir.pwd unless options.has_key?(:dest)
options[:source] = Dir.pwd unless options.has_key?(:source)
raise "Cannot read #{options[:source]}." unless
File.readable?(options[:source])
raise "Can't write to #{options[:dest]}." unless
File.writable?(options[:dest])
rescue RuntimeError => e
puts "Access failure: #{e}."
end

def walk_dir(dir, git_dirs)
tree_dirs = ['.', '..']
Dir.entries(dir).each {|node|
next if tree_dirs.include?(node)
node_ref = "#{dir}" + '/' + node
next unless File.executable?(node_ref)
if File.directory?(node_ref) and File.readable?(node_ref)
if node == '.git'
git_dirs << node_ref
else
walk_dir(node_ref, git_dirs)
end
end
}
end

walk_dir(options[:source], git_dirs)
puts git_dirs


Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cg
dogatana
2018-08-01 13:31:24 UTC
Permalink
Hi,
This
task is to find all the '.git' directories so I can grab the repository
URLs
and clone them into a new directory.
You can search all .git files in a directory and subdirectories like
irb
dir = '.'
=> "."
git_dirs = Dir["#{dir}/**/.git"]
=> ["./database/projectdb/.git", ...

Regards,
Toshihiko Ichida

Ken D'Ambrosio
2018-07-30 12:32:29 UTC
Permalink
Post by leam hall
Besides a brain, a clue. and a life, what am I missing?
Your problem almost certainly isn't with the code you show, which works
fine, but rather, with what's in options[:source] .
I'm betting there's something in there (perhaps even something dumb,
like a <CR>) that's not what you think it should be.
Or, possibly, it's an unfully qualified directory. But the code
directly below is spot-on; no changes required. Instead, add a line as I
did, and see what's going on.
Post by leam hall
Thanks!
Leam
###
puts "And here is what we think our PWD is: #{Dir.pwd} and our source
directory: #{options[:source]}."
Dir.entries(options[:source]).each {|node|
if File.directory?(node)
puts " node is: #{node}."
end
}
Post by leam hall
node is: ..
node is: ...
# If I remove the "File.directory?() it puts all the files and
directories.
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
leam hall
2018-07-30 13:39:36 UTC
Permalink
Post by Ken D'Ambrosio
Post by leam hall
Besides a brain, a clue. and a life, what am I missing?
Your problem almost certainly isn't with the code you show, which works
fine, but rather, with what's in options[:source] .
I'm betting there's something in there (perhaps even something dumb, like a
<CR>) that's not what you think it should be.
Or, possibly, it's an unfully qualified directory. But the code directly
below is spot-on; no changes required. Instead, add a line as I did, and see
what's going on.
Thanks, I'll give that a look The <CR> is likely the culprit.

In this case the command set options[:source] to the directory the
code was in, but used the explicit path "/home/me/lang/ruby".
Post by Ken D'Ambrosio
Post by leam hall
###
puts "And here is what we think our PWD is: #{Dir.pwd} and our source
directory: #{options[:source]}."
Dir.entries(options[:source]).each {|node|
if File.directory?(node)
puts " node is: #{node}."
end
}
Post by leam hall
node is: ..
node is: ...
# If I remove the "File.directory?() it puts all the files and directories.
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Loading...