标题: [原创代码] Perl对剪贴板文件批量重命名 [打印本页]
作者: 523066680 时间: 2014-8-23 19:19 标题: Perl对剪贴板文件批量重命名
环境, Active Perl for Win32
执行脚本开始循环读取剪切板待命,这时选中一批文件,然后Control+C ,即可实现对文件批量按序号命名。
默认是001 002 003 + .扩展名 如果使用命令行带参数调用,则用第一个参数作为前缀,
比如
───────────┐
Terminal>rn.pl PIC- │
───────────┘
则对读取到的文件列表命名为 PIC-编号.扩展名 的形式 还有很多可以完善的地方。执行后生成一个:repair_fname.pl
可以用于恢复最近一次修改的文件名。- use strict;
- use Encode;
- 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 $i;
- my $path_and_name;
- my $left = "";
- $left = shift @ARGV if (defined $ARGV[0]);
- $i = 0;
- foreach (@files) {
- if (! -e $_) {
- print "File not exists!\n";
- last;
- }
- $i++;
- /\.(.{1,3})$/;
- $path_and_name = sprintf(
- "%s\\%s%03d.%s",
- dirname($_), $left, $i, $1
- );
- if (-e $path_and_name) {
- print "WARNNING: The same file name does exists!\n";
- last;
- }
-
- print WRT "rename(\'$path_and_name\', \'$_\');" . "\r\n";
- print basename($_), " => ", basename($path_and_name) ,"\n";
- rename ($_, $path_and_name) or warn "$!";
- }
- close WRT;
- }
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |