[prog] bulk re-naming of files

Almut Behrens almut-behrens at gmx.net
Thu Jan 27 11:24:01 EST 2005


On Wed, Jan 26, 2005 at 06:11:42PM -0500, Wolf Rising wrote:
> Hi,
> Thank you for responding, I don't know anything about Ruby :-)
> Actually I don't know much
> about perl, so if it would be possible to run php from the command
> line to do the
> conversion I would think I might understand that the best. I ran the
> mysql select
> command and copied that information into a txt file. I would guess the
> steps might go
> something to the effect of:
> tell the script about the data file
> declare p_code, s_code, and file name as variables
> ask it to read from the data file 
> and when certain conditions are met, change the name of the file?
> I'm not very good at programming :-) Actually, not very good is an
> understatement :-)

Of course, there's always more than one way to do it (even if not using
perl ;)  If you just want to get the job done without having to learn
too much about programming, it's probably easiest to use the command
mysqldump. This is often used to backup whole databases. It essentially
dumps out "CREATE TABLE" and "INSERT" SQL statements, which, when fed
into the MySQL client, recreate the database with all tables and
contents (see mysqldump(1) for details).

So, you could do:

  mysqldump --opt yourdatabase > db.sql

Then do the necessary text replacements on the db.sql file (i.e. edit
the JPG names), before you run

  mysql yourdatabase < db.sql

The textual replacements can be automated with Perl, Python, Ruby,
vim, emacs, whatever...  If you want more help on that part, I'd
suggest you post a fragment of the db.sql file you dumped -- in
particular a few INSERT statements, so I can see the exact format. 
With that info, I could  tell you what the necessary replacement code
would look like...

BTW, you'll probably also want to rename the actual JPG files
themselves accordingly -- which can of course be done in one go...
(I assume you're keeping them outside of the databse in the filesystem,
or are you storing them inside the databse as BLOBs?)

Cheers,
Almut

 


More information about the Programming mailing list