如何访问子目录中的文件? [英] How to access files in subdirectories?

查看:215
本文介绍了如何访问子目录中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主目录中有几个子目录,每个子目录包含一个或两个文本文件。我需要将这些文本文件复制到另一个目录。

I have several subdirectories beneath in a main directory, and each subdirectory contains one or two text files. I need to copy these text files to another directory.

这是我的编码方式。但它似乎没有工作。我能做什么?有没有直接的方法这样做?

Here's how I coded. But it doesn't seem to work. What can I do? And is there a direct way of doing this?

#!/usr/bin/perl
use File::Copy;
my $directorypath;
opendir (DIR, '/d/work/abc') or die "cannot open path $!";
my @maindirectory = readdir (DIR);
closedir (DIR);

foreach my $subdirectorypath (@maindirectory)
{
    $subdirectorypath = join '', '/d/work/abc', $directorypath;
    chdir '$directorypath';
    my @textfile = glob "*.txt";
    foreach (@textfile)
    {
     $copiedfile = "/d/work/abcd/$_.txt."; #destination path 
     copy($_, $copiedfile) or die "File cannot be copied.";   
    }
}


推荐答案

在单行Perl程序中很简单

This is simple in a one-line Perl program

perl -MFile::Copy=copy -e'copy($_, "/d/work/abcd") for </d/work/abc/*/*.txt>'

这篇关于如何访问子目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆