简体   繁体   English

不需要在Perl中拆分文件名。 BUG?

[英]Unwanted splitting of file name in Perl. BUG?

I want to know the files within a directory tree. 我想知道目录树中的文件。 Something like this 像这样

TOP -> SECOND -> FOLDERS 1-3 -> NAME -> Files

I did not generate these files but they are formatted as such 我没有生成这些文件,但是它们被格式化为

1234 ACS. (Description).txt

Yes that is white space in there 是的,那里是空白

My issue is when I'm reading .txt files from the "NAME" folder 我的问题是,当我从“ NAME”文件夹中读取.txt文件时

opendir(DIR, $top.$second) or die "CANNOT OPEN SECOND DIRECTORY\n";
@nameFolders = grep { !/^\.|\.\.$/ }  readdir(DIR);
closedir(DIR);

foreach(@nameFolders){
        $folder = $_;
        if($_ =~ /\.txt$/){ next; } #sometimes .txt files are here but I took care of them earlier in the code and that works just fine
        #print $_."\n"; #Output is 100% perfect here
        opendir FIL, $top.$second."/".$folder or die "CANNOT OPEN NAME DIRECTORY\n";
        @files = grep  { /\.txt$/ } readdir(FIL);
        closedir(FIL);

        foreach(@files){
            $fileName = $_;
            print $fileName."\n"; #HERE IS MY PROBLEM OUTPUT IS BELOW
            @fileName = split / /, $fileName;
            $numID = $fileName[0];
            $goodFiles{$fileName}=$numID;
        }

}

OUTPUT: 
     1234 ACS. (STUFF).txt
     ACS.
     1235 ACS. (STUFF).txt
     ACS.
     ...

What is going on here? 这里发生了什么? I'm not spiting the file name until after I print, AND it's in the @files array. 在打印之后,我才吐出文件名,它位于@files数组中。

I'm at a loss. 我很茫然。

Thanks for any input. 感谢您的任何投入。

Does this help? 这有帮助吗?

use strict;
use warnings;

use File::Find;

my @directories_to_search = ();
push(@directories_to_search, 'TOP/SECOND/');
find(\&wanted, @directories_to_search);

sub wanted 
{
    my $file = $_;
    if (defined($file) && $file =~ m/.txt$/) 
    {
      print "$file\n";
    }
}

Output 输出量

1234 ACS. (STUFF).txt
1235 ACS. (STUFF).txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM