免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 8122 | 回复: 12
打印 上一主题 下一主题

perl 怎么得到一个目录下的所有子目录名 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-03 18:16 |只看该作者 |倒序浏览
写的简单点  我没做过perl  复杂了 不好理解

谢谢各位

论坛徽章:
0
2 [报告]
发表于 2010-09-03 18:48 |只看该作者
grep {-d} (<.*>,<*>)

论坛徽章:
0
3 [报告]
发表于 2010-09-03 18:49 |只看该作者
這個你search一把,結果一堆。。

论坛徽章:
0
4 [报告]
发表于 2010-09-03 19:18 |只看该作者
回复 1# zjm6533637

给点提示 http://www.tutorialspoint.com/perl/perl_files.htm
   
Working with Directories

Following are the standard functions used to play with directories.

opendir DIRHANDLE, EXPR  # To open a directory
readdir DIRHANDLE        # To read a directory
rewinddir DIRHANDLE      # Positioning pointer to the begining
telldir DIRHANDLE        # Returns current position of the dir
seekdir DIRHANDLE, POS   # Pointing pointer to POS inside dir
closedir DIRHANDLE       # Closing a directory.

Here is an example which opens a directory and list out all the files available inside this directory.

#!/usr/bin/perl

opendir (DIR, '.') or die "Couldn't open directory, $!";
while ($file = readdir DIR)
{
  print "$file\n";
}
closedir DIR;

Another example to print the list of C source code files, you might use

#!/usr/bin/perl

opendir(DIR, '.') or die "Couldn't open directory, $!";
foreach (sort grep(/^.*\.c$/,readdir(DIR)))
{
   print "$_\n";
}
closedir DIR;

You can make a new directory using the mkdir function:

To remove a directory, use the rmdir function:

To change the directory you can use chdir function.

论坛徽章:
0
5 [报告]
发表于 2010-09-03 23:36 |只看该作者
更复杂的File::Find模块可以用··

论坛徽章:
0
6 [报告]
发表于 2010-09-04 12:31 |只看该作者
回复  zjm6533637

给点提示
Perl_Er 发表于 2010-09-03 19:18



    你好,我想问下你这个可以达到遍历多层嵌套的目录吗? 即子目录下还有子目录

论坛徽章:
0
7 [报告]
发表于 2010-09-04 14:01 |只看该作者
回复 6# zjm6533637


不行,你用File::Find模块吧

论坛徽章:
0
8 [报告]
发表于 2010-09-05 21:47 |只看该作者
LS的 关键是这个模块我不会啊

论坛徽章:
0
9 [报告]
发表于 2010-09-05 22:02 |只看该作者
本帖最后由 paktc 于 2010-09-05 22:10 编辑

我来一个间接的,列出所有文件,然后判断是否为目录……
foreach (<*>) {
  if (-d $_) {print "$_\n"};
}

不过我发现可以控制层次
像这样,
foreach (<*/*/*>) {
  if (-d $_) {print "$_\n"};
}
会指定列出三层的目录。


遍历所有的话要么自己写递归,要么用模块吧
俺是学习中人,还没成的,模块还不会用。

论坛徽章:
0
10 [报告]
发表于 2010-09-07 17:05 |只看该作者
其实方法很多,比如楼上说的 find package,
其实如果你熟悉shell的话,也可以用system调用shell,比如 system “ls -lR|grep ^d|less”;
当然你也可以自己写一个递归,类似如:
#!/usr/bin/perl
sub get_dir () {
my @my_path = @_;
foreach my $file (@my_path) {
    if (-d $file) {
        print "Find The Dirfile\n";
         my @list=glob "$file/*";
         if ( ! @list) { #empty dir
              print "Find The Dirfile\n";
              } else {
                       &get_dir ( @list ); #recall get_dir
              }
           }
     }
}
&get_dir (glob " $target_dir/*";

.......


当然可能还有其他的更好的方式吧,我就不多说了。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP