Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, In my perl script I create a new directory and copy files into that directory. What I want to do then, is create another file which will list the names of all the other files in that directory. I know I would have to open this directory in order for its contents to be read, but how do I create the new file? thanks

Replies are listed 'Best First'.
Re: list files in a directory
by ChOas (Curate) on Jan 19, 2001 at 19:16 UTC
    I think you should have posted this Seekers
    for Perl wisdom... but anyways...

    sub ArchiveDir { my $Directory=shift||'.'; my $OutputFile=shift||'dircontents.txt'; opendir INDIR,$Directory or die "Cannot open $Directory: $!\n"; open OUTFILE,">$OutputFile" or die "Cannot open $OutputFile for outpu +t: $!\n"; print OUTFILE "$Directory/$_\n" for (readdir(INDIR)); close OUTFILE; closedir INDIR; };
    OR:
    sub ArchiveDir { my $Directory=shift||'.'; my $OutputFile=shift||'dircontents.txt'; open OUTFILE,">$OutputFile" or die "Cannot open $OutputFile for outpu +t: $!\n"; print OUTFILE "$_\n" for <$Directory/*>; close OUTFILE; };