遍历目录树 - Unicode 模式

  1. =info
  2.     遍历目录树 支持 Unicode
  3.     Code by 523066680@163.com
  4.     2017-03
  5.    
  6.     V0.5 使用Win32API判断目录硬链接
  7. = cut
  8.  
  9. use utf8;
  10. use Encode;
  11. use Win32API::File qw (GetFileAttributesW FILE_ATTRIBUTE_REPARSE_POINT);
  12. use Win32::Unicode;
  13. use IO::Handle;
  14. STDOUT- >autoflush(1);
  15. binmode (STDOUT, ':encoding(gbk)');
  16.  
  17. our $n_files = 0;
  18. our $n_dirs = 0;
  19.  
  20. my $path = "D:/Extra";
  21. func ($path, 0);
  22.  
  23. print $n_files ,"\n";
  24. print $n_dirs;
  25.  
  26. sub func
  27. {
  28.     my ($path, $lv) = (shift, shift);
  29.     my $wdir = Win32::Unicode::Dir->new;
  30.     my $code;
  31.     my $next_path;
  32.  
  33.     $wdir->open( $path );
  34.     if ( $wdir->error() =~ /找不到/ )
  35.     {
  36.         print $wdir->error();
  37.         exit;
  38.     }
  39.  
  40.     while ( my $f = $wdir->read() )
  41.     {
  42.         if ( file_type('f', $path. "/" .$f ) )
  43.         {
  44.             print "    "x$lv . "$f\n";
  45.             $n_files++;
  46.         }
  47.  
  48.         next if ($f eq ".");
  49.         next if ($f eq "..");
  50.  
  51.         $next_path = $path. "/" .$f;
  52.  
  53.         if ( file_type('d', $next_path ) )
  54.         {
  55.             $n_dirs++;
  56.             print "    "x$lv . "$f\n";
  57.             $code = GetFileAttributesW( encode('utf16-le', $next_path) ."\x00\x00" );
  58.  
  59.             if ( isLink( $code ) ) { print "skip symbolic link: $f\n"; }
  60.             else                   { func( $next_path,  $lv+1 );       }
  61.         }
  62.  
  63.     }
  64. }
  65.  
  66. sub isLink
  67. {
  68.     return ($_[0] & FILE_ATTRIBUTE_REPARSE_POINT) == FILE_ATTRIBUTE_REPARSE_POINT ?
  69.             1 : 0;
  70. }

转载于:https://www.cnblogs.com/catgatp/p/8443307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值