2

所以我有一个程序,我想清理一些文本文件。该程序要求用户输入包含这些文本文件的目录的完整路径。从那里我想读取目录中的文件,将它们打印到一个新文件(由用户指定),然后以我需要的方式清理它们。我已经编写了脚本来清理文本文件。

我要求用户提供要使用的目录:

chomp ($user_supplied_directory = <STDIN>); 
opendir (DIR, $user_supplied_directory);

然后我需要阅读目录。

my @dir = readdir DIR;

foreach (@dir) {

现在我迷路了。

请问有什么帮助吗?

4

4 回答 4

2

我不确定你想要什么。所以,我做了一些假设:

  • 当你说清理文本文件时,你的意思是删除文本文件
  • 您要写入的文件的名称由模式形成。

所以,如果我是对的,试试这样的:

chomp ($user_supplied_directory = <STDIN>);

opendir (DIR, $user_supplied_directory);
my @dir = readdir DIR;

foreach (@dir) {
    next if (($_ eq '.') || ($_ eq '..'));

    # Reads the content of the original file
    open FILE, $_;
    $contents = <FILE>;
    close FILE;

    # Here you supply the new filename
    $new_filename = $_ . ".new";

    # Writes the content to the new file
    open FILE, '>'.$new_filename;
    print FILE $content;
    close FILE;

    # Deletes the old file
    unlink $_;
}
于 2010-11-26T00:33:15.003 回答
2

我建议你切换到 File::Find。一开始可能有点挑战,但它功能强大且跨平台。

但是,要回答您的问题,请尝试以下操作:

my @files = readdir DIR;
foreach $file (@files) {
   foo($user_supplied_directory/$file);
}

其中“foo”是您需要对文件执行的任何操作。一些注意事项可能会有所帮助:

  • 使用“@dir”作为文件数组有点误导
  • 文件夹名称需要附加到文件名才能获得正确的文件
  • grep用它来丢弃不需要的文件和子文件夹可能很方便,尤其是“..”
于 2010-11-26T00:34:06.650 回答
1

我今天写了一些使用readdir. 也许你可以从中学到一些东西。这只是(有点)更大程序的一部分:

our @Perls = ();

{
    my $perl_rx = qr { ^ perl [\d.] + $ }x;
    for my $dir (split(/:/, $ENV{PATH})) {
        ### scanning: $dir
        my $relative = ($dir =~ m{^/});
        my $dirpath = $relative ? $dir : "$cwd/$dir";
        unless (chdir($dirpath)) {
            warn "can't cd to $dirpath: $!\n";
            next;
        }
        opendir(my $dot, ".") || next;
        while ($_ = readdir($dot)) {
            next unless /$perl_rx/o;
            ### considering: $_
            next unless -f;
            next unless -x _;
            ### saving: $_
            push @Perls, "$dir/$_";
        }
    }
}

{
    my $two_dots = qr{ [.] .* [.] }x;
    if (grep /$two_dots/, @Perls) {
        @Perls = grep /$two_dots/, @Perls;
    }
}

{
    my (%seen, $dev, $ino);
    @Perls = grep {
        ($dev, $ino) = stat $_;
        ! $seen{$dev, $ino}++;
    } @Perls;
}

关键是push(@Perls, "$dir/$_"):读取的文件readdir名只是基本名;它们不是完整的路径名。

于 2010-11-26T00:46:04.923 回答
0

您可以执行以下操作,允许用户提供自己的目录,或者,如果用户未指定目录,则默认为指定位置。

该示例显示了 , 的使用opendirreaddir将目录中的所有文件存储在@files数组中,并且仅以 '.txt' 结尾的文件存储在@keys数组中。while 循环确保文件的完整路径存储在数组中。

这假定您的“文本文件”以“.txt”后缀结尾。我希望这会有所帮助,因为我不太确定“清理文件”是什么意思。

use feature ':5.24';
use File::Copy;

my $dir = shift || "/some/default/directory";

opendir(my $dh, $dir) || die "Can't open $dir: $!";

while ( readdir $dh ) {
    push( @files, "$dir/$_");
}

# store ".txt" files in new array
foreach $file ( @files ) {
    push( @keys, $file ) if $file =~ /(\S+\.txt\z)/g;
}

# Move files to new location, even if it's across different devices
for ( @keys ) {
    move $_, "/some/other/directory/"; || die "Couldn't move files: $!\n";
}

有关更多信息,请参阅File::Copy 的 perldoc 。

于 2019-01-01T07:27:18.293 回答