[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

recursively descend and rename files.

Ezra Zygmuntowicz

7/7/2005 7:27:00 PM

Hello list-
Could someone please shed some light on this situation for me? I
have a huge folder of logos that are named all willy nilly with weird
characters and spaces. I need to recursively descend through all of
these folders and rename each image so the names only contain [a-
z0-9.-]. That part is fine I already have the regex ready to do this.
I need to know how to rename these files as I recurse through the
directory tree. I have this code so far:

path = "/Volumes/Users/ez/LOGOS"
files = []
require 'find'
Find.find(path) do |file|
files << file if file=~ /.eps$/
end



This leaves me with an array of good filenames but they have their
complete path as well. What I need help with is for each file, pul
just the file name out of the path, do some gsub's on it and then
rename it with the output of the gsubs. So after all is said and done
the original files will be renamed to be compliant with the regexes I
run on each one.

Can some one please point me in the right direction?

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



4 Answers

dblack

7/7/2005 7:38:00 PM

0

Martin DeMello

7/7/2005 7:54:00 PM

0

Ezra Zygmuntowicz <ezra@yakima-herald.com> wrote:
> Hello list-
> Could someone please shed some light on this situation for me? I
> have a huge folder of logos that are named all willy nilly with weird
> characters and spaces. I need to recursively descend through all of
> these folders and rename each image so the names only contain [a-
> z0-9.-]. That part is fine I already have the regex ready to do this.
> I need to know how to rename these files as I recurse through the
> directory tree. I have this code so far:
>
> path = "/Volumes/Users/ez/LOGOS"
> files = []
> require 'find'
> Find.find(path) do |file|
> files << file if file=~ /.eps$/
> end
>
>
>
> This leaves me with an array of good filenames but they have their
> complete path as well. What I need help with is for each file, pul
> just the file name out of the path, do some gsub's on it and then
> rename it with the output of the gsubs. So after all is said and done
> the original files will be renamed to be compliant with the regexes I
> run on each one.
>
> Can some one please point me in the right direction?

Check out the FileUtils module for the OS stuff like copying and moving,
and File.basename for the path splitting.

martin

Ryan Leavengood

7/7/2005 7:55:00 PM

0

Ezra Zygmuntowicz said:
>
> Can some one please point me in the right direction?

require 'ftools'

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

def fix_name(basename)
# whatever fixes the name
basename.gsub(/s/,'xxx')
end
__END__

Ryan


Ezra Zygmuntowicz

7/7/2005 9:21:00 PM

0

Thanks for everyones Ideas on this one. It works perfectly. If anyone
has the need to do something similar heres the code:

---start--------
#!/usr/local/bin/ruby
# recursively descend through directories and change filenames to be
webserver compliant based on a regex.

require 'find'
require 'ftools'


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.-]*/, '')
name.gsub!(/^-/,'')
end

path = "/Volumes/Users/ez/LOGOS" # change me to the path of the root
folder you want to start from.
Find.find(path) do |file|
rename(file)
end

----end---------

On Jul 7, 2005, at 12:54 PM, Ryan Leavengood wrote:

> Ezra Zygmuntowicz said:
>
>>
>> Can some one please point me in the right direction?
>>
>
> require 'ftools'
>
> def rename(filename)
> new_name = File.join(File.dirname(filename),
> fix_name(File.basename(filename)))
> File.move(filename, new_name)
> end
>
> def fix_name(basename)
> # whatever fixes the name
> basename.gsub(/s/,'xxx')
> end
> __END__
>
> Ryan
>

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