Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Run a perl script on multiple files?

  • 21-07-2005 02: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, Registered Users 2 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, Registered Users 2 Posts: 6,721 ✭✭✭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, Registered Users 2 Posts: 3,890 ✭✭✭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
Advertisement