mysql - 根据数据库中的值重命名文件

标签 mysql perl rename

我对 Perl 非常陌生,我需要完成一个依赖它的项目。我需要拍摄文件夹中的图片,在数据库中查找它们的名称,然后根据该行中的另一个值重命名图片。

所以说我需要重命名图片

010300000000001002.jpg

然后我需要让脚本在数据库中查找

010300000000001002

用户ID中。然后,当它找到匹配项时,我需要它在同一行中查找 EMPNUM 值。然后获取 EMPNUM 值并将图片重命名为 value.jpg,在本例中等于 1002。因此最终产品将如下所示:

Old Picture: 010300000000001002.jpg
New Picture: 1002.jpg

然后对该文件夹中的所有图片重复此操作。

从数据库脚本读取:

#!/usr/bin/perl -w
use strict;

use DBI;
# Replace datasource_name with the name of your data source.  AdventureWorksDW \dbo.DimEmployee
# Replace database_username and database_password
# with the SQL Server database username and password.
my $data_source = q/not giving the data source/;
my $user = q/Not giving the user/;
my $password = q/Not Giving the password /;
my $dbh = DBI->connect($data_source, $user, $password)
    or die "Can't connect to $data_source: $DBI::errstr";

# Catch and display status messages with this error handler.
sub err_handler {
   my ($sqlstate, $msg, $nativeerr) = @_;
   # Strip out all of the driver ID stuff
   $msg =~ s/^(\[[\w\s:]*\])+//;
   print $msg;
   print "===> state: $sqlstate msg: $msg nativeerr: $nativeerr\n";
   return 0;
}

$dbh->{odbc_err_handler} = \&err_handler;

$dbh->{odbc_exec_direct} = 1;

# Prepare your sql statement (perldoc DBI for much more info).
my $sth = $dbh->prepare('select userid, empnum from dbo.emp0');

# Execute the statement.
if ($sth->execute)
{
    # This will keep returning until you run out of rows.
    while (my $row = $sth->fetchrow_hashref)
    {
        print "ID = $row->{userid}, Name = $row->{empnum}\n";
    }
}

$dbh->disconnect;

复制并重命名文件脚本:

#!/usr/bin/perl -w
use strict;
use warnings;

my $i = 1;
my @old_names = glob "/root/pics/*.jpg";


foreach my $old_name (@old_names) {

    my $new_name = "picture$i" . ".jpg";

    rename($old_name, "/root/pics/$new_name") or die "Couldn't rename $old_name to $new_name: $!\n";

} continue { $i++ }

print "Pictures have been renamed.\n";

编辑: 这就是我最终得到的结果,它完成了工作。

#!/usr/bin/perl -w
use strict;
use File::Copy;
use DBI;

my $data_source = q/db/;
my $user = q/user/;
my $password = q/password/;
my $dbh = DBI->connect($data_source, $user, $password)
    or die "Can't connect to $data_source: $DBI::errstr";

# Catch and display status messages with this error handler.
sub err_handler {
   my ($sqlstate, $msg, $nativeerr) = @_;
   # Strip out all of the driver ID stuff
   $msg =~ s/^(\[[\w\s:]*\])+//;
   print $msg;
   print "===> state: $sqlstate msg: $msg nativeerr: $nativeerr\n";
   return 0;
}
$dbh->{odbc_err_handler} = \&err_handler;
$dbh->{odbc_exec_direct} = 1;

sub move_image 
{
    my $user_id = $_[0];
    my $emp_num = $_[1];

    my $old_name = "/root/picS/${user_id}.jpg";

    my $new_name = "/root/picD/${emp_num}.jpg";

    move($old_name, $new_name) or return 0;

    return 1;
}

# Prepare your sql statement (perldoc DBI for much more info).
my $sth = $dbh->prepare('select userid, empnum from dbo.emp0');

# Execute the statement.
if ($sth->execute)
{
    # This will keep returning until you run out of rows.
    while (my $row = $sth->fetchrow_hashref)
    {
        my $user_id = $row->{userid};
        my $emp_num = $row->{empnum};

        if (move_image($user_id, $emp_num))
        {
            print "Moved image $user_id to $emp_num successfully\n";
        }
        else
        {
            print "Could not move image $user_id to $emp_num successfully\n";
        }
    }
}

$dbh->disconnect;

最佳答案

首先,我建议使用 move From File::Copyrename subroutine其使用时间有限制。但是,如果您确定两个图像始终位于同一文件夹中,那么rename应该没问题,甚至可能更高效。

将以下内容添加到代码顶部以便能够使用 File::Copy:

use File::Copy;

其次,如果你想集成这里的功能,我会将“移动”代码放入子例程中。

sub move_image 
{
    my $user_id = $_[0];
    my $emp_num = $_[1];

    my $old_name = "/root/pics/${user_id}.jpg";

    my $new_name = "/root/pics/picture${emp_num}.jpg";

    move($old_name, $new_name) or return 0;

    return 1;
}

然后调用子例程:

# This will keep returning until you run out of rows.
while (my $row = $sth->fetchrow_hashref)
{
    my $user_id = $row->{userid};
    my $emp_num = $row->{empnum};

    if (move_image($user_id, $emp_num))
    {
        print "Moved image $user_id to $emp_num successfully\n";
    }
    else
    {
        print "Could not move image $user_id to $emp_num successfully\n";
    }
}

只需将子例程添加到您的原始脚本中,就像我所示的那样。不要尝试使用两个不同的脚本。 Subroutines非常有用。

关于mysql - 根据数据库中的值重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12982185/

相关文章:

php - 在没有 AJAX 的情况下将网格数据传递到 Bootstrap 模式?

php - Laravel:在表中的同一 ID 下逐一显示多行

linux - 在 Linux Centos 7 下安装 tcl/tk 以与 Perl 5 一起使用 - 缺少一个步骤?

linux - 如何仅从其他列中查找和排序值

version-control - "abuse"Mercurial 的重命名功能可以跟踪代码块的移动吗?

java - 如何使用Java重命名目录中的所有文件?

mysql - 质心方法是否在 MySQL 空间中实现?

避免重新定义已定义函数的 Perl 库

bash - 从文件名中删除日期但保留文件扩展名

mysql - 在 AngularJS 中调用 NodeJS 函数