为该目录以及子目录添加index.html

add index.html to a directory recursively

using Perl5

使用的目录,是从Perl下载的perl5.18.2的文档
Look Here

#!/usr/bin/perl
# filename : ban.pl
#为指定目录以及其子目录,每个都添加一个index.html裱糊文件
use IO::Dir;
use IO::File;
my $indent = 0;
&add_index_html("$ARGV[0]");
sub touch {
#相当于一个touch命令
   my $fh = IO::File->new("index.html",'w');
   if ($fh) {
       print $fh "<h1>Back to <a href=\"#\">RenLab</a></h1>";
       $fh->close;
   }
}
sub add_index_html {
    my $dir  = shift;
    my $d = IO::Dir->new("$dir");
    if (defined($d)) {
        chdir $dir;
        print STDERR "$dir\n";
        &touch;
        while (defined($_ = $d->read)) {
            next if ($_ =~ /^\.\.?$/);
            #跳过 . 和 .. 目录
            #不然,一直循环 . 目录
            if (-d "$_") {
            #如果是一个子目录
                $indent++;
                print STDERR "  " x $indent;
                #每一层目录缩进2个空格
                chmod 0755, "$_";
                &add_index_html("$_");
                #也可以使用 __SUB__ feature
                #处理子目录
                $indent--;
            }else{
                next
            }
        }
        chdir("..");
    }
}

现在,你可以

perl ban.pl perdoc5.18.2
perldoc5.18.2
  App
    Prove
      State
        Result
  Archive
    Tar
  Attribute
  B
    Lint
  CGI
  Class
  Compress
    Raw
  Config
  CPAN
  CPANPLUS
    Backend
    Dist
      Build
    Internals
      Source
    Module
      Author
    Shell
      Default
        Plugins
  Data
  DBM_Filter
  Devel
  Digest
  Encode
    CN
    JP
    KR
    MIME
    Unicode
  Exporter
  ExtUtils
    CBuilder
      Platform
    Command
    Constant
    MakeMaker
  File
    Spec
  Filter
    Util
  functions
  Getopt
  Hash
    Util
  I18N
    LangTags
  IO
    Compress
    Socket
    Uncompress
  IPC
  List
    Util
  Locale
    Maketext
  Log
    Message
  Math
    BigInt
  Memoize
  MIME
  Module
    Build
      Platform
    Load
    Pluggable
  Net
  Object
  Package
  Params
  Parse
    CPAN
  PerlIO
    via
  Pod
    Perldoc
    Simple
    Text
  Scalar
  Search
  static
  Sys
  TAP
    Formatter
      Console
      File
    Parser
      Iterator
      Result
      Scheduler
      YAMLish
  Term
    UI
  Test
    Builder
      Tester
  Text
  Thread
  threads
  Tie
    Hash
  Time
  Unicode
  User
  warnings
posted @ 2016-07-01 23:45  乌祁班岚图  阅读(836)  评论(0编辑  收藏  举报