我对 Perl 很陌生,我需要完成一个依赖它的项目。我需要拍摄文件夹中的图片,在数据库中查找它们的名称,然后根据该行中的另一个值重命名图片。
所以说我需要重命名图片
010300000000001002.jpg
然后我需要让脚本在数据库中查找
010300000000001002
在USERID
. 然后,当它找到匹配项时,我需要它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;