recursively descend and rename files.

  • Thread starter Ezra Zygmuntowicz
  • Start date
E

Ezra Zygmuntowicz

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
(e-mail address removed)
 
D

David A. Black

Hi --

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?

You could just do the operation as you go:

Find.find(path) do |file|
fix(file) if /.eps$/.match(file) # or whatever
end

The fact that it's got the path attached shouldn't matter for the
renaming operation, since you're not chdir'ing.


David
 
M

Martin DeMello

Ezra Zygmuntowicz said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top