reading a list of files

Discussion in 'Perl Misc' started by john williams, Oct 4, 2004.

  1. Hi,

    I have a list of files:

    basename_001.ext
    ....
    basename_1000.ext

    I would like to be able to read them one by one, and process each
    file, then write the files one by one as:

    basename_out_0001.ext
    ....
    basename_out_1000.ext


    due to the large number of files, I need this to be automated.

    Any help would be appreciated, thanks in advance
    John
    john williams, Oct 4, 2004
    #1
    1. Advertising

  2. john williams wrote:
    > I have a list of files:
    >
    > basename_001.ext
    > ...
    > basename_1000.ext
    >
    > I would like to be able to read them one by one, and process each
    > file, then write the files one by one as:
    >
    > basename_out_0001.ext
    > ...
    > basename_out_1000.ext
    >
    > due to the large number of files, I need this to be automated.


    What you want to do is possible, and Perl is a possible programming
    language.

    > Any help would be appreciated,


    Help with what? Writing your program for you? No way!

    Please study the posting guidelines for this group:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    I don't know how much Perl you know, but maybe you need to start here:

    http://learn.perl.org/

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 4, 2004
    #2
    1. Advertising

  3. john williams <> wrote:

    > I have a list of files:
    >
    > basename_001.ext
    > ...
    > basename_1000.ext
    >
    > I would like to be able to read them one by one,



    perldoc -f opendir
    perldoc -f readdir
    perldoc -f closedir
    perldoc -f glob


    > and process each
    > file, then write the files one by one as:



    perldoc -f rename
    perldoc -f open


    > basename_out_0001.ext
    > ...
    > basename_out_1000.ext



    See the "s/PATTERN/REPLACEMENT/egimosx" section in:

    perldoc perlop


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 4, 2004
    #3
  4. john williams

    gumby Guest

    (john williams) wrote in message news:<>...
    > Hi,
    >
    > I have a list of files:
    >
    > basename_001.ext
    > ...
    > basename_1000.ext
    >
    > I would like to be able to read them one by one, and process each
    > file, then write the files one by one as:
    >
    > basename_out_0001.ext
    > ...
    > basename_out_1000.ext
    >
    >
    > due to the large number of files, I need this to be automated.
    >
    > Any help would be appreciated, thanks in advance
    > John


    Try this:
    my @filesToBeProcessed = <$directoryToBeProcessed/*.ext >
    foreach(@filesToBeProcessed)
    {
    #open each file and do what you need then close the file.
    }

    Should work for you globs the directory you specify and puts the files
    with path into the array. Then step through the array and do what
    ever you need to with each file.
    gumby, Oct 4, 2004
    #4
  5. john williams

    krakle Guest

    (john williams) wrote in message news:<>...
    > Hi,
    >
    > I have a list of files:
    >
    > basename_001.ext
    > ...
    > basename_1000.ext


    Great.. If all the files are in a single directory throw all the file
    names in an array...

    my @filenames = </some/path/to/dir/*>;

    Then loop through the array and perform whatever functions you want
    to...

    foreach my $file (@filenames) {
    # some yadda perl
    }
    >
    > I would like to be able to read them one by one,


    open (FILE, $file) || die $!;
    @src = <FILE>;
    close (FILE);

    -or-


    open (FILE, $file) || die $!;
    while (<FILE>) {
    $src .= $_;
    }
    close (FILE);


    > and process each
    > file,


    Process each file as in???

    > then write the files one by one as:
    >
    > basename_out_0001.ext
    > ...
    > basename_out_1000.ext


    This is all plain ol' simple Perl...

    open (FILE, ">$file") || die $!; # Over write.. use >> to append
    print FILE "whatever the hell you want\n";
    close (FILE);

    >
    >
    > due to the large number of files, I need this to be automated.


    Automated as in ran on a schedule? cronjob...

    >
    > Any help would be appreciated, thanks in advance
    > John


    Actually, since this is all very simple you could of done it yourself
    if you know basic Perl. Looks like you just wanted someone to write it
    for you.
    krakle, Oct 5, 2004
    #5
  6. Pinocchio <> wrote:

    > my @filenames = </some/path/to/dir/*>;
    >
    > Then loop through the array and perform whatever functions you want
    > to...
    >
    > foreach my $file (@filenames) {
    > # some yadda perl
    > }



    @filenames is serving no useful purpose.

    foreach my $file ( </some/path/to/dir/*> ) { # Look Ma! No temp vars!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 6, 2004
    #6
  7. john williams

    krakle Guest

    Tad McClellan <> wrote in message news:<>...
    > Pinocchio <> wrote:
    >
    > > my @filenames = </some/path/to/dir/*>;
    > >
    > > Then loop through the array and perform whatever functions you want
    > > to...
    > >
    > > foreach my $file (@filenames) {
    > > # some yadda perl
    > > }

    >
    >
    > @filenames is serving no useful purpose.
    >
    > foreach my $file ( </some/path/to/dir/*> ) { # Look Ma! No temp vars!


    Yes this is true.
    My example was just an idea made in 10 seconds..
    krakle, Oct 11, 2004
    #7
  8. john williams

    Gerhard M Guest

    (john williams) wrote in message news:<>...
    > ....
    > due to the large number of files, I need this to be automated.


    # ls basename* | perl -ne 'chomp; /(.*)\_(.*)/ && {print "YOUR_CMD $_
    > $1_out_$2\n" | sh
    Gerhard M, Oct 12, 2004
    #8
  9. john williams

    Gerhard M Guest

    (john williams) wrote in message news:<>...
    > ....
    > due to the large number of files, I need this to be automated.


    # ls basename* | perl -ne 'chomp; /(.*)\_(.*)/ && {print "YOUR_CMD $_
    > $1_out_$2\n";}' | sh
    Gerhard M, Oct 12, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick
    Replies:
    3
    Views:
    3,691
  2. rob hadow

    JAR files reading list files

    rob hadow, May 21, 2004, in forum: Java
    Replies:
    4
    Views:
    4,766
    rob hadow
    May 21, 2004
  3. Ajay  Reddy
    Replies:
    1
    Views:
    546
    Rhino
    Feb 9, 2005
  4. Replies:
    0
    Views:
    771
  5. Narendra Sisodiya

    Reading files inside zip files from Javascript

    Narendra Sisodiya, Dec 7, 2009, in forum: Javascript
    Replies:
    7
    Views:
    275
    Michael Haufe (\TNO\)
    Dec 8, 2009
Loading...

Share This Page