[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Go through directories recursively

Jens Riedel

5/12/2005 10:38:00 AM

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
16 Answers

Robert Klemme

5/12/2005 11:24:00 AM

0

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

Kind regards

robert

Robert Klemme

5/12/2005 11:26:00 AM

0

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
>
> Kind regards
>
> robert

PS: You can also do

Dir["**/*.jsp"].each do |f|
if File.file? f
# do something with f
end
end

dblack

5/12/2005 11:26:00 AM

0

riccardo

5/12/2005 11:26:00 AM

0

Hi Jens

one possible solution is to use the Find library

require 'find'

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"
way
> to do this...
>
> Thanx for every hint,
> Jens

Robert Klemme

5/12/2005 11:33:00 AM

0

David A. Black wrote:
> HI --
>
> On Thu, 12 May 2005, 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...
>
> 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

Hm, this might be worthwile to go into find.rb...

Kind regards

robert

Jens Riedel

5/12/2005 1:07:00 PM

0

Thanx for all of your hints, I chose the Find.find method.

Regards,
Jens

Ara.T.Howard

5/12/2005 2:10:00 PM

0

nobu.nokada

5/12/2005 3:01:00 PM

0

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.

--
Nobu Nakada


Ara.T.Howard

5/12/2005 3:27:00 PM

0

Nakada, Nobuyoshi

5/13/2005 5:18:00 AM

0

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.

--
Nobu Nakada