Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

execut perl script for multiple input folders

by lakssreedhar (Acolyte)
on Jul 19, 2013 at 08:36 UTC ( [id://1045295]=perlquestion: print w/replies, xml ) Need Help??

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

I a perl code which takes 2 inputs. perl run.pl sentence/1.txt name/1.txt >output/1.txt I need to execute the above program to 1000 files in sentence folder and 1000 files in name folder and get the 1000 files output in output folder.Is it possible to do it with cmd command.

Replies are listed 'Best First'.
Re: execut perl script for multiple input folders
by Corion (Patriarch) on Jul 19, 2013 at 09:12 UTC

    An easy approach to globbing is to use File::Glob::bsd_glob:

    use File::Glob qw( bsd_glob ); # Wildcard-expand @ARGV: BEGIN { @ARGV= map { bsd_glob $_ } @ARGV }
Re: execut perl script for multiple input folders
by vinoth.ree (Monsignor) on Jul 19, 2013 at 08:57 UTC

    I guess you have 1000 files in sentence folder want to execute run.pl script on each file and create output of each file into output directory with the same input file name?

    This post will help you, Input Output Question

    Use opendir and readdir with while loop on sentence folder so you will get all the input file names, and pass those input filenames to your run.pl as command line argument.

    Sample Code:

    #!/usr/bin/perl use strict; use warnings; my $directory = '/sentence'; opendir (DIR, $directory) or die $!; while (my $file = readdir(DIR)) { #Run your script here with ``(backtick) or system() } closedir(DIR);
    Update:

    If you want to pass all the 1000 files to your run.pl script, in the while loop push each file name into an array then call your script as `run.pl @filenames`

    Even you can also use glob to get the list of files from a directory.

    OR

    Use File::Slurp::read_dir to get the list of files:

    use File::Slurp; my @files = read_dir '/sentence';

    All is well
Re: execut perl script for multiple input folders
by mtmcc (Hermit) on Jul 19, 2013 at 09:10 UTC
    Something like this should work:

    #! /usr/bin/perl use strict; use warnings; opendir(DIR, "/full/path/to/sentence/"); my @filesInDirectoryA = readdir(DIR); opendir(DIR, "/full/path/to/name/"); my @filesInDirectoryB = readdir(DIR); my $count = @filesInDirectoryA; my $x = 0; for ($x = 0; $x<$count; $x += 1) { if ($filesInDirectoryA[$x] ne $filesInDirectoryB[$x]) { die "mismatched files!\n\n"; } } for ($x = 0; $x<$count; $x += 1) { unless ($filesInDirectoryA[$x] =~ m/^\./) { system("perl run.pl /full/path/to/sentence/$filesInDir +ectoryA[$x] /full/path/to/name/@filesInDirectoryB > /full/path/to/out +put/$filesInDirectoryA[$x]"); } }

    That should take all the files in the sentence and name directories and feed them to your script. Provided that the files in each of the directories have the same name. You need to replace the path names.

Re: execut perl script for multiple input folders
by kcott (Archbishop) on Jul 19, 2013 at 09:06 UTC

    G'day lakssreedhar,

    That doesn't look like a Perl question at all: please mark it as OT. [In case you didn't know, OT = Off Topic]

    The answer will depend on your OS. On a *nix system, I'd probably use for:

    $ for i in {1..2}; do echo $i; done 1 2

    -- Ken

      That doesn't look like a Perl question at all:

      Sure does, why use shell to call perl program when you can just call a perl program?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found