Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226553
  • 博文数量: 36
  • 博客积分: 1188
  • 博客等级: 军士长
  • 技术积分: 802
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 21:45
文章分类

全部博文(36)

文章存档

2020年(1)

2017年(2)

2015年(1)

2014年(1)

2013年(1)

2012年(3)

2011年(27)

分类: WINDOWS

2011-08-10 21:23:04

功能与XP系统的xcopy命令相似
  1. #use Acme::PerlTidy;

  2. package Backup;
  3. use Moose;
  4. use AnyEvent;
  5. use File::Xcopy qw(xcopy find_files);

  6. has from_dir => ( is => 'rw' );
  7. has to_dir => ( is => 'rw' );
  8. has regex => ( is => 'rw', isa => 'Str', required => 1, default => '.*' );

  9. #&menu( $prompt, @a);
  10. sub Menu {
  11.     my $self = shift;
  12.     print '', "\n";
  13.     my $prompt = 'which will you chose:';
  14.     my @array = qw(FindFiles Backup Exit);
  15.     print " $_:$array[$_]\n" for ( 0 .. $#array );

  16.     my $cv = AnyEvent->condvar;
  17.     my $w;
  18.     $w = AnyEvent->idle(
  19.         cb => sub {
  20.             print $prompt;
  21.             my $n = <>;
  22.             if ( $n =~ /^\d+$/ and defined $array[$n] ) {
  23.                 print "You chose:$array[$n]\n";
  24.                 my $block = '_' . $array[$n];
  25.                 eval $self->$block;
  26.             }
  27.         }
  28.     );

  29.     $cv->recv;

  30. }

  31. #备份文件
  32. sub _Backup {
  33.     my $self = shift;
  34.     my $fx = File::Xcopy->new;
  35.     my %p = ( s => 1 );
  36.     $fx->xcp( $self->from_dir, $self->to_dir, $self->regex, \%p );
  37. }

  38. #列出文件清单
  39. sub _FindFiles {
  40.     my $self = shift;
  41.     my $fx = File::Xcopy->new;
  42.     my $ar = $fx->find_files( $self->from_dir, $self->regex );
  43.     open my $fh, ">目录.txt";
  44.     for my $ar_file ( @{$ar} ) {
  45.         print "\t$ar_file->{path}\n";
  46.         print $fh "$ar_file->{path}\n";
  47.     }
  48.     close $fh;
  49. }

  50. sub _Exit { my $self = shift; exit 0 }

  51. #如不存在目标备份文件夹(copy_to_dir)则新建同名文件夹
  52. around to_dir => sub {
  53.     my ( $orig, $self, @args ) = @_;
  54.     my $dir = $self->$orig(@args);
  55.     if ( !-e $dir ) {
  56.         mkdir($dir);
  57.     }
  58.     return $dir;
  59. };

  60. package main;

  61. #备份文件支持正则表达式,只备份doc文档时regex属性设置为:regex => '.*\.doc$'
  62. #my $bac = Backup->new( from_dir => '', to_dir => '',regex => '');
  63. #修改下一句-----------------------------------------------------------
  64. my $bac = Backup->new( from_dir => 'D:/结果', to_dir => 'G:/结果',regex => '.*' );

  65. $bac->Menu();
阅读(1919) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~