- 论坛徽章:
- 0
|
本帖最后由 aef25u 于 2011-06-06 16:04 编辑
以下代码适用于XP系统- use Cwd;
- use Win32::OLE;
- use Moo***;
- #use Data::Dumper;
- #############################################################################
- class SearchFile {
- has regex =>
- ( is => 'rw', isa => 'Str', required => 1, default => '.*\.doc$' );
- #弹出窗口,以供选择目标文件夹
- method _Path {
- my $shell = Win32::OLE->new("shell.Application");
- my $objFolder = $shell->BrowseForFolder( 0, "Select a folder:", 0 );
- my $Path = $objFolder->Self->{Path};
- $Path =~ s/(\\)/\//g;
- return $Path;
- }
- #遍历搜索文件,支持正则表达式
- method File_Find {
- my $tem = $self->regex;
- my $qr_regex = qr/$tem/;
- my $Path = $self->_Path();
- my @dirs = ( $Path . '/' );
- my ( @retFile, $dir, $file );
- while ( $dir = pop(@dirs) ) {
- local *DH;
- if ( !opendir( DH, $dir ) ) {
- warn "Cannot opendir $dir: $! $^E";
- next;
- }
- foreach ( readdir(DH) ) {
- if ( $_ eq '.' || $_ eq '..' ) {
- next;
- }
- $file = $dir . $_;
- if ( $file =~ /$qr_regex/ ) {
- push( @retFile, $file );
- }
- if ( !-l $file && -d _ ) {
- $file .= '/';
- push( @dirs, $file );
- }
- }
- closedir(DH);
- }
- return \@retFile;
- }
- }
- #############################################################################
- my $objFind=SearchFile->new(regex=>'.*\.xls');
- #my $objFind=SearchFile->new();
- my $ffn=$objFind->File_Find();
- print join("\n",@{$ffn});
复制代码 第三行用
|
|