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

Hello Friends,
Does anyone know how to open a folder rather than a simple file (text file or otherwise)? I'm not sure about the basic syntax of such a thing. What I am trying to accomplish is to parse out data within a series of text files found in a folder. However I'm not sure of how to perform a task on a number of files that will vary. Normally I have the user type in the file to parse on the command prompt. This program needs to be automated to the point of taking a given folder (stated by user), and assigning a filehandle to each text file. Once this is done, I need to be able to reference each so my basic parsing element can act on each file and converge the data into one output file. The actual parsing element is only a few lines long; getting all the data in and referencing the filehandles is the issue. Thanks so much for your help!!!

Bioinformatics

Replies are listed 'Best First'.
Re: Variable numbers of folders...
by sauoq (Abbot) on Jul 25, 2003 at 18:45 UTC
    This program needs to be automated to the point of taking a given folder (stated by user), and assigning a filehandle to each text file. Once this is done, I need to be able to reference each so my basic parsing element can act on each file and converge the data into one output file.

    If I read that right, you want to have all of the filehandles at the same time, presumably so you can read from each as you need to rather than process the files sequentially. This mostly untested code:

    sub get_filehandles { my $dir = shift; opendir my ($dh), $dir; my @fhs = map { my $fh; open( $fh, '<', "$dir/$_" ) ? $fh : do { warn "$dir/$_: $!"; () } } grep { -f "$dir/$_" } readdir($dh); closedir $dh; return \@fhs; }
    will return a reference to an array of filehandles open for reading. (You must pass in the path to the directory where the files are.) Keep in mind that you may have limits on the number of filehandles you can have open at once.

    Update: Fixed a bug. I was always returning the filehandle whether it was successfully opened or not. It shouldn't barf with warnings on now. I also tried to improve its readability a bit.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Variable numbers of folders...
by LazerRed (Pilgrim) on Jul 25, 2003 at 18:39 UTC
    Something like:

    opendir(DIR, $directory) or die "Failed to open $directory: $!"; while (defined($filename = readdir(DIR))) { #Do stuff to each file $filename } closedir(DIR);
    You could easily wrap in a foreach block if you have multiple directories specified.

    LR

    Whip me, Beat me, Make me use Y-ModemG.

    Update: Also, I might add, Learning Perl and Perl Cookbook are invaluable resources to have on hand.
      I have tried to implement your suggested code into the program, but have found it isn't workig very well. Apparently, it is unable to open the directory. This could be because I'm not entering the name in a readable form, but I'm not sure. Here's where I'm at:
      #! usr/local/bin/perl print STDOUT "Hello\!\n"; print STDOUT "This program will take a folder of Affymetrix text files + and put\n"; print STDOUT "their signal data in a format useful for Cyber-T\.\n"; print STDOUT "Please enter the name and location of the directory to p +arse\:\n"; $directory=<STDIN>; open (OUTPUTFILE,">data.csv"); opendir(DIR, "$directory") or die "Failed to open $directory: $!"; while (defined(@filename = readdir(DIR))) { @spliced_data=splice(@filename, 0, 15); foreach $_(@filename) { ($a, $b, $c, $d, $e, $f)=split(/\t/); push(@final_data, "$d\n");} print OUTPUTFILE "@final_data"; } closedir(DIR);
      Thanks for your help!!