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

Greetings monks!

I'm in need of a quick script if anyone has a moment. What I have is a directory structure with a handful of files in it. I'm looking for something that'll do the equivalent of "find *index.ccr" and just send the output of that command to a textfile called indexlist.ccr.

I'm sure this is absurdly easy, but my googling hasn't lead me to anything that looks like it'd do what I need. Any advice would really be appreciated. :) Thanks!
  • Comment on Beginner question regarding file lists/output

Replies are listed 'Best First'.
Re: Beginner question regarding file lists/output
by choedebeck (Beadle) on Dec 05, 2005 at 21:45 UTC
    I imagine the File::Find module is what you are looking for.
Re: Beginner question regarding file lists/output
by ickyb0d (Monk) on Dec 05, 2005 at 21:46 UTC
    If all the files are in your current directory this should work. Are there any recursive directories that need to be searched? If you need to search a specific directory, just change the "." to whatever directory you want. Hope this helps.
    #opening dir n' grabbing files opendir(DIR, "."); my @files = grep(/^.+?index\.ccr$/,readdir(DIR)); closedir(DIR); #opening file for writing and outputting to indexlist.ccr open(FILE, ">indexlist.ccr") foreach(@files) { print FILE $_ . "\n"; } close(FILE);
Re: Beginner question regarding file lists/output
by swampyankee (Parson) on Dec 05, 2005 at 22:24 UTC

    If you're in a *ixish world, why not just ls *index.ccr > indexlist.ccr?

    In the Windows world, I think substituting dir for ls, with some appropriate options, may work.

    If you're trying to do this from within a Perl program, I'd try something like

    #perl -w use strict; use diagnostics; use File::Glob qw(:globally :nocase); my $log = 'indexlist.ccr'; open(LOG, $log) or die "Could not open $log because $!\n"; my @list = <*index.ccr>; print LOG join("\n", @list) . "\n"; close(LOG);

    If you're running Windows, you'd probably want to use File::DosGlob;, with appropriate adjustments, instead.

    emc

    Netlib
      In the Windows shell, this would achieve the desired:

      dir *index.ccr > indexlist.ccr

      Or you could use pipes, and filter dir through find and then output to the file, like so:

      dir | find "index.ccr" > indexlist.ccr

      Chaining commands using pipes allows the Windows shell, even though severy crippled when compared with *nix shells, to perform moderatelly complex administrative tasks.