#!/usr/bin/perl use DBI; $dsn = "DBI:mysql:gallery:localhost"; $user_name = "gallery"; $password = "******"; $dbh = DBI->connect ($dsn, $user_name, $password); opendir(DH, ".") or die("opendir"); @folders = readdir(DH); closedir(DH); FOLDER: foreach $folder (@folders) { unless ($folder =~ /\d+/) {next FOLDER;} print "$folder\n"; opendir(DH, "$folder") or die("opendir"); @files = readdir(DH); closedir(DH); FILE: foreach $file (@files) { if ($file =~ /^\./) {next FILE;} $sth = $dbh->prepare ("select title from images where set_id = '$folder' and filename like '$file%'"); $sth->execute (); $rv = $sth->bind_columns(\$title); chomp $title; $sth->fetch(); $title =~ s/.jpg$//i; $title =~ s/[^0-9A-Za-z ]//g; $newfile = $title . ".JPG"; $command = "mv \"$folder/$file\" \"$folder/$newfile\""; print "$command\n"; system($command); } }