[Perl]rnfiles.pl 访问剪切板的文件列表并批量重命名

There's more than one way to do it!
https://metacpan.org http://perlmonks.org
回复
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

[Perl]rnfiles.pl 访问剪切板的文件列表并批量重命名

帖子 523066680 »

=info
rename files by clipboard
523066680 / Code-By.Org
2014-08
=cut

=usage
建议放在运行环境中/PATH的某些工具目录中

调用方法:rnfiles.pl 前缀-
回车,然后选中你要批量重命名的文件,按 Ctrl+C 或者 鼠标右键-复制
注意事项:选中的文件应是相同格式的,脚本不做检查,后果自负

如果发生误操作,你仍有一次修复机会,执行后会生成逆操作脚本:repair_fname.pl
=cut


use strict;
use Encode;
use utf8;
use Win32::Clipboard;
use File::Basename qw/dirname basename/;
use IO::Handle;
use Time::HiRes 'sleep';
STDOUT->autoflush(1);

my $clip;
our @files;
$clip = Win32::Clipboard();

$clip->Empty();
while (scalar(@files) <= 1)
{
@files = $clip->GetFiles();
print ".";
sleep 0.5;
}
print "Got it! \n";
&deal();

sub deal
{
open WRT, ">:raw", dirname(__FILE__)."\\repair_fname.pl" or die "WHAT? $!";
our @files;
my $num;
my $path_and_name;
my $left = "";
$left = shift @ARGV if (defined $ARGV[0]);
$num = 0;
my $len;
$len = length( $#files+1 );

for my $f (@files)
{
if (! -e $f)
{
print "File not exists!\n";
last;
}
$num++;

$f =~/\.(\w+)$/;
$path_and_name = sprintf(
"%s\\%s%0${len}d.%s",
dirname($f), $left, $num, $1
);

if (-e $path_and_name)
{
print "WARNNING: Same filename does exists!\n";
last;
}

print WRT "rename(\'$path_and_name\', \'$f\');" . "\r\n";
print basename($f), " => ", basename($path_and_name) ,"\n";
rename ($f, $path_and_name) or warn "$!";
}
close WRT;
}
示例:
图片
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 6 访客