Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Run a perl script on multiple files?

Options
  • 21-07-2005 3:44pm
    #1
    Closed Accounts Posts: 16


    I need to run a perl script on every file in a directory. It needs to read each file and for each file create an output file in a different directory with the same name as the original file.

    I can't use the opendir command because i can't figure out how to get the name of the file then. (which i need to create the an output file with the same name.

    Basically the whole operation works fine for one file but not when i try

    perl script.pl *.*

    at the command prompt (Linux).

    There must be a simple way to do this but i can't find it.

    Thanks!


Comments

  • Registered Users Posts: 441 ✭✭robfitz


    You should be able to do some with the following sample code which loops over the argument list and prints out just the basename of the file.
    #!/bin/perl
    use File::Basename;
    for (my $i = 0; $i < @ARGV; $i++) {
            my $file = $ARGV[$i];
            print basename($file), "\n";
    }
    


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    lzhn wrote:
    Basically the whole operation works fine for one file but not when i try
    How about you post your code (or portions at least) and we might be able to enable multiple file handling.
    robfitz's suggestion of going through @ARGV should fix it.

    This creates an empty file for each file in the source directory.
    It doesn't deal with '*.*' but demonstrates opendir/readdir/closedir.
    #!/usr/bin/perl -w
    use strict;
    
    my $logdir = 'logdir';
    my $srcdir = 'srcdir';
    
    if ( opendir( DH, $srcdir ) )
    {
      while ( readdir( DH ) )
      {
        if ( -f "$srcdir/$_" )
        {
          open( FH, ">$logdir/$f" );  # Error checking skipped.
          close( FH );
        }
      }
      closedir( DH );
    }
    
    using shell...
    export srcdir=/path/to/srcdir
    export logdir=/the/dir/for/logs
    cd $srcdir
    for f in *.*
    do
      if [ -f $f ]
      then
        touch $logdir/$f
      fi
    done
    


  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    find ./ -type f -exec perl script.pl {} \;
    
    will find, recursively all files in the current dir, and pass each of them (1 at a time) .. e.g.
    perl script test1.txt
    perl script test2.txt
    ...
    


  • Closed Accounts Posts: 16 lzhn


    cgarvey wrote:
    find ./ -type f -exec perl script.pl {} \;
    
    will find, recursively all files in the current dir, and pass each of them (1 at a time) .. e.g.
    perl script test1.txt
    perl script test2.txt
    ...
    

    Absolute magic! Thank you!


Advertisement