I'm quite new to Ruby.
I'd to walk through a directory tree and edit all jsp files in there.
I do it this way:
def editDirectory(path)
Dir.entries(path).each { |filename|
next if filename =~ /^\.+$/
currFile = path + "/" + filename
if(File.stat(currFile).directory?)
editDirectory(currFile)
elsif(filename =~ /(.+)\.jsp$/)
# ... do something with the file
end
}
end
I'd like to know if there is a more professional or more "ruby-like" way to do this...
I'm quite new to Ruby.
I'd to walk through a directory tree and edit all jsp files in there.
I do it this way:
def editDirectory(path)
Dir.entries(path).each { |filename|
next if filename =~ /^\.+$/
currFile = path + "/" + filename
if(File.stat(currFile).directory?)
editDirectory(currFile)
elsif(filename =~ /(.+)\.jsp$/)
# ... do something with the file
end
}
end
I'd like to know if there is a more professional or more "ruby-like"
way to do this...
Thanx for every hint,
Jens
require 'find'
def edit_directory(dir)
Find::find(dir) do |f|
if /\.jsp$/i =~ f and File.file? f
# do something with f
end
end
end
I'm quite new to Ruby.
I'd to walk through a directory tree and edit all jsp files in there.
I do it this way:
def editDirectory(path)
Dir.entries(path).each { |filename|
next if filename =~ /^\.+$/
currFile = path + "/" + filename
if(File.stat(currFile).directory?)
editDirectory(currFile)
elsif(filename =~ /(.+)\.jsp$/)
# ... do something with the file
end
}
end
I'd like to know if there is a more professional or more "ruby-like" way to do this...
You can use the 'find' module. Here's a parameterized version, which
takes as its arguments a path and a file extension, and yields back
the filenames it finds one by one:
require 'find'
def find_by_extension(path, ext)
Find.find(path) do |f|
next unless FileTest.file?(f)
next unless /#{Regexp.escape(ext)}$/.match(f)
yield f
end
end
# Example of usage:
find_by_extension("/home/dblack", ".rb") do |f|
# do stuff with f
end
Find::find(path){|file|
next if !File.basename(x).include?(".jsp")
## do what you need on jsp files
}
where path is the starting dir where you start the search
Don't know if it is more ruby, but for me is working quite well.
See you, Riccardo
Jens Riedel wrote:
Hello,
I'm quite new to Ruby.
I'd to walk through a directory tree and edit all jsp files in there.
I do it this way:
def editDirectory(path)
Dir.entries(path).each { |filename|
next if filename =~ /^\.+$/
currFile = path + "/" + filename
if(File.stat(currFile).directory?)
editDirectory(currFile)
elsif(filename =~ /(.+)\.jsp$/)
# ... do something with the file
end
}
end
I'd like to know if there is a more professional or more "ruby-like"
I'm quite new to Ruby.
I'd to walk through a directory tree and edit all jsp files in there.
I do it this way:
def editDirectory(path)
Dir.entries(path).each { |filename|
next if filename =~ /^\.+$/
currFile = path + "/" + filename
if(File.stat(currFile).directory?)
editDirectory(currFile)
elsif(filename =~ /(.+)\.jsp$/)
# ... do something with the file
end
}
end
I'd like to know if there is a more professional or more "ruby-like"
way to do this...
Thanx for every hint,
Jens
require 'find'
def edit_directory(dir)
Find::find(dir) do |f|
if /\.jsp$/i =~ f and File.file? f
# do something with f
end
end
end
Kind regards
robert
PS: You can also do
Dir["**/*.jsp"].each do |f|
if File.file? f
# do something with f
end
end
I'm quite new to Ruby.
I'd to walk through a directory tree and edit all jsp files in there.
I do it this way:
def editDirectory(path)
Dir.entries(path).each { |filename|
next if filename =~ /^\.+$/
currFile = path + "/" + filename
if(File.stat(currFile).directory?)
editDirectory(currFile)
elsif(filename =~ /(.+)\.jsp$/)
# ... do something with the file
end
}
end
I'd like to know if there is a more professional or more "ruby-like"
way to do this...
You can use the 'find' module. Here's a parameterized version, which
takes as its arguments a path and a file extension, and yields back
the filenames it finds one by one:
require 'find'
def find_by_extension(path, ext)
Find.find(path) do |f|
next unless FileTest.file?(f)
next unless /#{Regexp.escape(ext)}$/.match(f)
yield f
end
end
# Example of usage:
find_by_extension("/home/dblack", ".rb") do |f|
# do stuff with f
end
David
Even more generic:
module Find
def self.find_cond(dir, cond)
find(dir){|f| yield f if cond === f}
end
end
Find::find_cond ".", /\.jsp$/ do |f|
puts f
end
# and including the file type test
proper_test = Object.new
def proper_test.===(f) /\.jsp$/ =~ f and File.file? f end
Find::find_cond ".", proper_test do |f|
puts f
end
i've had both following and not-following links cause frustrating bugs - so
it's good to be aware of which/when you need the behaviour.
cheers.
-a
···
On Thu, 12 May 2005, Robert Klemme wrote:
Jens Riedel wrote:
Hello,
I'm quite new to Ruby.
I'd to walk through a directory tree and edit all jsp files in there.
I do it this way:
def editDirectory(path)
Dir.entries(path).each { |filename|
next if filename =~ /^\.+$/
currFile = path + "/" + filename
if(File.stat(currFile).directory?)
editDirectory(currFile)
elsif(filename =~ /(.+)\.jsp$/)
# ... do something with the file
end
}
end
I'd like to know if there is a more professional or more "ruby-like"
way to do this...
Thanx for every hint,
Jens
require 'find'
def edit_directory(dir)
Find::find(dir) do |f|
if /\.jsp$/i =~ f and File.file? f
# do something with f
end
end
end
--
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
renunciation is not getting rid of the things of this world, but accepting
that they pass away. --aitken roshi
Robert Klemme wrote:
>
> require 'find'
>
> def edit_directory(dir)
> Find::find(dir) do |f|
> if /\.jsp$/i =~ f and File.file? f
> # do something with f
> end
> end
> end
>
> Kind regards
>
> robert
PS: You can also do
Dir["**/*.jsp"].each do |f|
if File.file? f
# do something with f
end
end
Question: what if the operation you want to perform is to delete
directories or files? From my understanding of Find#find, it yields
directories breadth-firsh rather than depth first. So if you want to
delete a directory yielded to your block, do you have to call
Find#prune on it to avoid messing up the traversal?
Likewise for Dir["**/*.jsp"]. Does this form first create an internal
array of file names before yielding them, or does it yield each file as
it inspects each directory? If the latter, does the traversal get
messed up if you delete a file out from under it?
I didn't know about either of the two above options, so the first time
I needed to traverse directories recursively (to delete files, no less)
I rolled my own solution, shown below. Until I know that either of the
above options is safe for this, I will continue to use it.
# Yields the name of each directory within the specified directory
# recursively. Order is depth-first, to accommodate deleting files in
the
# yielded directory from within the block. Passed in directory is
yielded last
def each_dir(base, &block)
Dir.entries(base).each do |n|
unless (n == '.') || (n == '..') # Don't traverse . or ..
n = File.join(base, n) unless base == '.'
if File.directory?(n)
each_dir(n, &block)
yield n
end
end
end
yield base
end
from the find2 code i've incorporated into my personal lib (Alib):
···
On Fri, 13 May 2005 nobu.nokada@softhome.net wrote:
Hi,
At Thu, 12 May 2005 23:25:28 +0900,
Ara.T.Howard@noaa.gov wrote in [ruby-talk:142376]:
one thing to consider is that neither of these approaches follows links:
It can cause infinite recursion.
#
# If `entry_path' is a directory, find recursively.
#
if stat_result.directory? \
&& (!@xdev || @xdev_device == stat_result.dev) \
&& (!@follow || !visited?(stat_result)) @dirname_stats.push(stat_result)
find_directory(entry_path, block) @dirname_stats.pop
end
kind regards.
-a
--
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
renunciation is not getting rid of the things of this world, but accepting
that they pass away. --aitken roshi
Question: what if the operation you want to perform is to delete
directories or files? From my understanding of Find#find, it yields
directories breadth-firsh rather than depth first. So if you want to
delete a directory yielded to your block, do you have to call
Find#prune on it to avoid messing up the traversal?
Likewise for Dir["**/*.jsp"]. Does this form first create an internal
array of file names before yielding them, or does it yield each file
as it inspects each directory? If the latter, does the traversal get
messed up if you delete a file out from under it?
Dir always creates the array first so this is save.
Question: what if the operation you want to perform is to delete
directories or files? From my understanding of Find#find, it yields
directories breadth-firsh rather than depth first. So if you want to
delete a directory yielded to your block, do you have to call
Find#prune on it to avoid messing up the traversal?
Likewise for Dir["**/*.jsp"]. Does this form first create an internal
array of file names before yielding them, or does it yield each file
as it inspects each directory? If the latter, does the traversal get
messed up if you delete a file out from under it?
Dir always creates the array first so this is save.
--
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
renunciation is not getting rid of the things of this world, but accepting
that they pass away. --aitken roshi
and ruby then becomes useless for doing find type operations in this
environment since any view of the fs stops dead at a link. in any case, i
certainly can (have) dealt with it by using Find2 - is there any reason this
module, or something similar, shouldn't be included in the core?
kind regards.
-a
···
On Fri, 13 May 2005, Nakada, Nobuyoshi wrote:
Hi,
At Fri, 13 May 2005 00:45:27 +0900,
Ara.T.Howard@noaa.gov wrote in [ruby-talk:142400]:
from the find2 code i've incorporated into my personal lib (Alib):
#
# If `entry_path' is a directory, find recursively.
#
if stat_result.directory? \
&& (!@xdev || @xdev_device == stat_result.dev) \
&& (!@follow || !visited?(stat_result))
This "visited?" method would be the key. We know it is possible, but
judged it is too expensive to implement as built-in.
--
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
renunciation is not getting rid of the things of this world, but accepting
that they pass away. --aitken roshi