Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Problems? Is your data what you think it is?
 
PerlMonks  

Input Output Question

by perlmonk007 (Novice)
on Jul 15, 2013 at 04:08 UTC ( #1044261=perlquestion: print w/ replies, xml ) Need Help??
perlmonk007 has asked for the wisdom of the Perl Monks concerning the following question:

Hey Guys I had a simple question. I have three folders input, output and code. I need the code in the code folder and I want to parse all the files from the input folder (no pattern for file names) and get one output for each file with the same filename in the output folder. The code should run in any computer if I send the folders.

Could anyone please help me with this, any good libraries that I could use to achieve this???

Comment on Input Output Question
Re: Input Output Question
by vinoth.ree (Vicar) on Jul 15, 2013 at 04:52 UTC

    To read all the files in a directory use the directory functions: opendir(), closedir() and readdir().

    opendir DIR, $dir or die "cannot open dir $dir: $!"; my @file= readdir + DIR; closedir DIR;
    @file array will contains the files available in input directory parse each file and redirect output to output directory with the same file name.


    All is well

      thanks, how do I output the files one by one? could you please give me an example?

        Simply use the foreach loop to parse each file and redirect the output to output directory

        For Example:
        my $output_dir = 'output/'; foreach my $file (@files) { print $file . "\n"; #Parse the file here open(FH, '>>', $output_dir.$file ) or die "Unable to open the +file: $!"; print FH "Output goes into output directory with the same file + name\n"; close(FH); }

        All is well
Re: Input Output Question
by rjt (Chaplain) on Jul 15, 2013 at 06:02 UTC

    Here's a complete example of a search/replace using opendir. This will replace tabs with spaces in the input directory, saving the result in the output directory. Both directories must already exist. Hopefully this will illustrate the structure you can follow. Note that the "code" directory is irrelevant (as far as the script is concerned).

    #!/usr/bin/env perl use 5.012; use warnings; use autodie; use Getopt::Long; # Replace tabs with 4 spaces my $verbose = 0; GetOptions(verbose => \$verbose); die "Usage: $0 [--verbose] input_path output_path" if @ARGV != 2; map { die "$_ is not a directory, or does not exist\n" unless -d } @AR +GV; my ($input, $output) = @ARGV; opendir my ($in_dir), $input; for (sort grep { -f "$input/$_" } readdir $in_dir) { say if $verbose; open my $in, '<', "$input/$_"; open my $out, '>', "$output/$_"; while (<$in>) { s/\t/ /sg; print $out $_; } close $in; close $out; } closedir $in_dir;

      thank you, I am still trying to figure this out will let you know how it goes

Re: Input Output Question
by poj (Curate) on Jul 15, 2013 at 06:18 UTC

    Assuming you want the code in the code folder

    #!perl use strict; use File::Copy; opendir(DIR,'../input') or die "$!"; while (readdir DIR){ unless (/^[.]{1,2}$/){ print "copying $_\n"; copy("../input/$_","../output/$_") or die "Copy failed for $_ : $!"; } }
    poj

      Thanks for the reply, but doesnt this code just copy the files. I want to parse the input for information

        Yes, it just copies the files. If you want to change them in the process then something like this
        #!perl use strict; opendir(DIR,'../input') or die "$!"; while (readdir DIR){ unless (/^[.]{1,2}$/){ print "parsing $_\n"; open IN,'<','../input/'.$_ or die "Could not open $_ for input : $!"; open OUT,'>','../output/'.$_ or die "Could not open $_ for output : $!"; while (my $line = <IN>){ # process $line print OUT $line; } close IN; close OUT; } }
        poj
Re: Input Output Question (folders files, Path::Class, Path::Tiny )
by Anonymous Monk on Jul 15, 2013 at 06:55 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1044261]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-04-21 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (491 votes), past polls