How to access files in subdirectories?

寵の児 提交于 2019-12-02 16:46:45

问题


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.";   
    }
}

回答1:


This is simple in a one-line Perl program

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


来源:https://stackoverflow.com/questions/30196894/how-to-access-files-in-subdirectories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!