2

私は Perl に非常に慣れていないので、Perl に依存するプロジェクトを完了する必要があります。フォルダーにある写真を撮り、DB で名前を検索してから、その行の別の値に基づいて写真の名前を変更する必要があります。

画像の名前を変更する必要があるとします

010300000000001002.jpg

次に、スクリプトでデータベースを検索する必要があります

010300000000001002

USERIDEMPNUM次に、一致が見つかったら、同じ行の値を探す必要があります。次に、EMPNUM値を取得して、画像の名前をその value.jpg に変更します。この場合は 1002 です。したがって、最終製品は次のようになります。

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

次に、そのフォルダー内のすべての写真に対して繰り返します。

DB スクリプトから読み取ります。

#!/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;
4

1 に答える 1

2

moveまず、 From File::Copyを使用することをお勧めします。名前変更サブルーチンには、いつ使用できるかについての制限があります。ただし、両方の画像が常に同じフォルダにあることが確実な場合は、rename問題がなく、さらに効率的である可能性があります。

使用できるようにするには、コードの先頭に以下を追加しますFile::Copy

use File::Copy;

次に、ここで機能を統合したい場合は、「move」コードをサブルーチンにします。

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";
    }
}

私が示したように、サブルーチンを元のスクリプトに追加するだけです。2つの異なるスクリプトを使用しようとしないでください。サブルーチンは非常に便利です。

于 2012-10-19T21:44:48.680 に答える