[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to tell Dir from file?

Ezra Zygmuntowicz

7/11/2005 9:54:00 PM

Hello list-
I have the following script that recursively descends through
some directories and changes the name of a bunch of logos to be
webserver compliant.

require 'find'
require 'ftools'
path = "/Volumes/Users/ez/LOGOS/"

def rename(filename)
new_name = File.join(File.dirname(filename), fix_name(File.basename
(filename)))
File.move(filename, new_name)
end

def fix_name(name)
name.gsub!(/[\s_]/, "-")
name.downcase!
name.gsub!(/[^a-z0-9.-]*/, '')
end

Find.find(path) do |file|
rename(file)
end

I need to know how to tell the difference between a file with no
extension and a directory name. The script works perfectly right now
except when it first runs into a directory it makes a 0 byte file
with the same name within that directory. I need to know how to test
whether the current iteration contains an actual file or is a
directory so I can skip renaming it.

You help is much appreciated.


-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
ezra@yakima-herald.com



1 Answer

Guillaume Marcais

7/11/2005 10:05:00 PM

0

Have a look at 'ri test' or 'ri File.directory?'.

HTH,
Guillaume.

On Tue, 2005-07-12 at 06:54 +0900, Ezra Zygmuntowicz wrote:
> Hello list-
> I have the following script that recursively descends through
> some directories and changes the name of a bunch of logos to be
> webserver compliant.
>
> require 'find'
> require 'ftools'
> path = "/Volumes/Users/ez/LOGOS/"
>
> def rename(filename)
> new_name = File.join(File.dirname(filename), fix_name(File.basename
> (filename)))
> File.move(filename, new_name)
> end
>
> def fix_name(name)
> name.gsub!(/[\s_]/, "-")
> name.downcase!
> name.gsub!(/[^a-z0-9.-]*/, '')
> end
>
> Find.find(path) do |file|
> rename(file)
> end
>
> I need to know how to tell the difference between a file with no
> extension and a directory name. The script works perfectly right now
> except when it first runs into a directory it makes a 0 byte file
> with the same name within that directory. I need to know how to test
> whether the current iteration contains an actual file or is a
> directory so I can skip renaming it.
>
> You help is much appreciated.
>
>
> -Ezra Zygmuntowicz
> Yakima Herald-Republic
> WebMaster
> 509-577-7732
> ezra@yakima-herald.com
>
>