Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Running Batch of Files

by tej (Scribe)
on Oct 07, 2010 at 11:37 UTC ( #863982=perlquestion: print w/ replies, xml ) Need Help??
tej has asked for the wisdom of the Perl Monks concerning the following question:

I want my script to convert many files in a batch

Right now, i am running one file through command prompt with one argument as input file

Please help me with this

Thank You

Comment on Running Batch of Files
Re: Running Batch of Files
by marto (Chancellor) on Oct 07, 2010 at 11:44 UTC

    I'm not sure I understand this question. Do you want your script to run against many files, rather than one file at a time? If so why not alter your script to read in a list of files (perhaps from a file or command line) or to process all files stored within a defined sub directory?

      Extreamly Sorry for confusion..But you got it right.

      I want to run my script agains many files. And i want it to do it through script, but dont know how to do it.

Re: Running Batch of Files
by zentara (Archbishop) on Oct 07, 2010 at 11:47 UTC
    Maybe something like this:
    #!/usr/bin/perl my $dir = '.'; my $pattern = '.jpg$'; #Filenames ending in .jpg opendir DIR, $dir or die "Cannot readdir $dir:$!\n"; my @files = grep /$pattern/,(readdir DIR); closedir DIR; print "@files\n"; foreach my $file (@files){ system ("somecommand $file"); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thanks zentara..This is almost working fine

      Only problem is i am getting error like can not open file..

      My code is as below

      my $dir = "INPUT"; my $pattern = '.txt'; opendir DIR, $dir or die "cant open dir"; my @files = grep /\.txt/,(readdir DIR); closedir DIR; #print "@files\n"; foreach my $file (@files){ print "$file\n"; open (INF, $file)||warn "Can not open $file"; $data=join ("", <INF>); print "$data\n"; }
      when i run this file i get error like "Can not open file" Could you please help me with this?

        Your problem is that you are only using the filename to open the file not the fullpath.

        my $dir = "INPUT"; my $pattern = '.txt'; opendir DIR, $dir or die "cant open dir"; my @files = grep /\.txt/,(readdir DIR); closedir DIR; #print "@files\n"; foreach my $file (@files){ my $filepath = "$dir/$file"; print "$filepath\n"; open (INF, $filepath)||warn "Can not open $filepath"; $data=join ("", <INF>); print "$data\n"; }

Re: Running Batch of Files
by eff_i_g (Curate) on Oct 07, 2010 at 14:07 UTC

      I have all input files in Input DIR and output files in Output Dir

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-20 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (155 votes), past polls