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: get contents of directory
by KM (Priest) on Jan 19, 2001 at 20:42 UTC
    perldoc -f open
    perldoc perlopentut
    perldoc -f opendir (and readdir)
    Those should enlighten you.

    Cheers,
    KM

Re: get contents of directory
by Beatnik (Parson) on Jan 19, 2001 at 21:17 UTC
    Besides the usual opendir(), readdir() and closedir() routine to read a directory's contents, you could also use glob(), but it's pretty slow. Opening a file for writing, like mentioned above, can be achieved with open().
    opendir(DIR,"/somedir") || die $!; @Files = readdir(DIR); closedir(DIR);

    Greetz
    Beatnik
    . .. Quidquid perl dictum sit, altum viditur.
Re: get contents of directory
by mr.nick (Chaplain) on Jan 19, 2001 at 22:15 UTC
    This is fairly standard, but I assume you want to create the list of files so that it DOESN'T contain the filename of the list of files? (grin)

    my $fn="files.txt"; open OUT,">$fn" || die $!; print OUT grep(!/^$fn$/,map { $_.="\n"; $_ } <*>); close OUT;

    This assumes that the current working directory is the one you wish to index. If it is not:

    my $dir="thedir"; my $fn="files.txt"; open OUT,">$dir/$fn" || die $!; print OUT grep(!/^$fn$/,map { s/^$dir\///; $_.="\n"; $_ } <$dir/*>); close OUT;
      This may be slightly off topic, it sounds to me you're creating something similar to a MANIFEST file. Let me explain what this is..

      On CPAN, every module has a file in it called MANIFEST. It's become a standard part of most perl module distributions. Basically, it's is a list of all the files in the current (and lower directories), along with optional comments beside each file name. It's main purpose is to be a module-wide database of required files, so that during install, any missing files will immediately raise errors from the install process.

      There is a module called ExtUtils::Manifest, which is part of the standard perl distribution, that takes care of reading the current directory, and creating a MANIFEST file for you. As well, inside a file called MANIFEST.SKIP, you can specify a list of regexes that match file names to skip.

      While it's interface it's very elegant, it is functional, and may give you ideas if/when you decide to roll your own manifest writer. Here's some simple example code that will create a MANIFEST with the current directories' contents inside it:

      #!/usr/bin/perl -w use ExtUtils::Manifest qw(mkmanifest); #Optional Configuration $ExtUtils::Manifest::Quiet = 1; #Turn off warnings $ExtUtils::Manifest::Verbose = 0; #Turn off status updates $ExtUtils::Manifest::MANIFEST = 'MANIFEST'; #Filename to write #Will make a MANIFEST file for all the files in your #current directory, including the MANIFEST file mkmanifest();