I have some logo images in the following directory structure:
/users/a/adam/logos/jdkajsdns.jpg
/users/a/alan/logos/skdjaksxa.jpg
(…)
/users/b/bob(…)
(…)
etc.
(there are also other directories and files in the structure and some
directories are empty; but I’d like to extract just the “branch” with
logo pictures)
Names of directories like “adam”, “alan” are users logins in the
database. I need to transform that structure into something like this:
/logos/756/jdkajsdns.jpg
/logos/815/skdjaksxa.jpg
etc.
where 756 is the id of the user ‘adam’ and the 815 is the id of the user
‘alan’, etc.
So:
It could be possible to bind logins and ids by generating a text file
with pairs of these values from db and to write a script that transforms
the structure using such file. How should the proper ruby script look
like?
Is it possible to use script/console in Rails and solve the problem
just inside it?
Users.find(:all).each do |u|
mkdir “/logos/#{u.id}”
cp_r “/users/#{u.login[0…0]}/#{u.login}/logos/.”, “/logos/#{u.id}”
end
Thanx for reply. It almost works but… what if a particular user does
not have logo image or even he doesn’t have path like above? I supposed
there should be Dir.glob with “empty?” used somewhere in the code.
I wrote something like this:
require “fileutils”
include FileUtils
User.find(:all,:limit=>‘20’).each do |u|
mkdir “public/uploaded/logos/#{u.id}”
cp_r “public/uploaded/users/#{u.login[0…0]}/#{u.login}/logo/.”,
“public/uploaded/logos/#{u.id}” unless
Dir.glob(“public/uploaded/users/#{u.login[0…0]}/#{u.login}/logo”).empty?
end
Ugly but it seems to be working.
I have another question: how to force this script to copy only jpg, png
and gif files from the “logo” directory? (there are also some mouldy php
files I don’t need ANYMORE)
Le Thu, 2 Apr 2009 06:20:56 -0500, Emmek On rails a écrit :
I have another question: how to force this script to copy only jpg, png
and gif files from the “logo” directory? (there are also some mouldy php
files I don’t need ANYMORE)
Even easier : iterate over Dir.glob(“path/to/orig/**/*.{jpg,png,gif}”).
(Add the File::FNM_CASEFOLD flag as a parameter if you have mixed case.)
but I don’t know what to put at the end of this path to copy just jpgs,
gifs and pngs.
It won’t work that way ; cp_r copies the whole directories. You need to
use Dir.glob or Find.find to select the files and then copy them one by
one.
Again, pretty much untested :
Dir.chdir("/public/uploaded/users/#{u.login[0…0]}/#{u.login}/logo")
do
Dir.glob("**/*.{jpg,png,gif}").each do |f|
f2 = “/public/uploaded/logos/#{u.id}/#{f}”
mkdir_p File.dirname(f2)
cp_r f, f2
end
end
(You’ll need at least to specify absolute paths if public isn’t in the
root.)