Copy and change file extension

Discussion in 'Perl' started by b.milbrandt, Apr 19, 2005.

  1. b.milbrandt

    b.milbrandt Guest

    I am trying to convert a unix shell script to perl, and I have having a
    problem with the portion that copies files based on extension and renaming
    them in the process. I only trying to copy files with a non-zero length.
    Here is what I have so far.

    $target is a command line parameter
    $source is a command line parameter

    opendir DH, "/$target";
    foreach $file (readdir DH)
    { printf " the file name is %s\n", $file;
    next unless $file =~ \/.abc$/ and !-z $name;
    my $newfile = /$target//$file;
    $newfile =~ s/\.abc$/.xyz/;
    $filecount += 1;
    }
    b.milbrandt, Apr 19, 2005
    #1
    1. Advertising

  2. b.milbrandt

    Jim Gibson Guest

    In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
    <> wrote:

    > I am trying to convert a unix shell script to perl, and I have having a
    > problem with the portion that copies files based on extension and renaming
    > them in the process. I only trying to copy files with a non-zero length.
    > Here is what I have so far.
    >
    > $target is a command line parameter
    > $source is a command line parameter
    >
    > opendir DH, "/$target";
    > foreach $file (readdir DH)
    > { printf " the file name is %s\n", $file;
    > next unless $file =~ \/.abc$/ and !-z $name;
    > my $newfile = /$target//$file;
    > $newfile =~ s/\.abc$/.xyz/;
    > $filecount += 1;
    > }


    Is this the actual program you are trying to run? You are missing
    characters and using the wrong characters in some places. You do not
    say what this program is doing wrong. How can anybody help you?

    Please post a complete, working, short-as-possible, cut-and-pasted
    program that demonstrates the problem you are having, but do it in
    comp.lang.perl.misc because this newsgroup is defunct.

    Thanks.


    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
    Jim Gibson, Apr 20, 2005
    #2
    1. Advertising

  3. b.milbrandt

    b.milbrandt Guest

    Here is the whole program

    #!/bin/perl
    # Check to be sure exactly 2 arguments passed to script
    die "Must pass exactly 2 arguments to script" if @ARGV != 2;

    # assign meaningfull names to variables
    $source = @ARGV[0];
    $target = @ARGV[1];

    # Check to see if First argument is a directory
    # if not exit
    die "$source is not a directory \n" if !-d $source;


    mkdir $target, 0755;

    # if !-d $target;
    #{ # Create Target Directory
    # mkdir $target, 0755;
    #}

    #copy all files with a .abc extension to the target directory
    # renaming the extension to xyz


    # chdir $source;
    # rename -f

    $filecount = 0;

    opendir DH, "//$source";
    foreach $file (readdir DH)
    { printf " the file name is %s\n", $file;
    next unless $file =~ \/.abc$/ and !-z $name;
    my $newfile = /$target//$file;
    $newfile =~ s/\.abc$/.xyz/;
    $filecount += 1;
    }

    # Print the number of files copied
    printf "The number of files copied is: %d\n", $filecount;

    The program runs but I get 0 files copied and no files are copied or
    renamed.

    Brian

    "Jim Gibson" <> wrote in message
    news:190420051608396664%...
    > In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
    > <> wrote:
    >
    >> I am trying to convert a unix shell script to perl, and I have having a
    >> problem with the portion that copies files based on extension and
    >> renaming
    >> them in the process. I only trying to copy files with a non-zero length.
    >> Here is what I have so far.
    >>
    >> $target is a command line parameter
    >> $source is a command line parameter
    >>
    >> opendir DH, "/$target";
    >> foreach $file (readdir DH)
    >> { printf " the file name is %s\n", $file;
    >> next unless $file =~ \/.abc$/ and !-z $name;
    >> my $newfile = /$target//$file;
    >> $newfile =~ s/\.abc$/.xyz/;
    >> $filecount += 1;
    >> }

    >
    > Is this the actual program you are trying to run? You are missing
    > characters and using the wrong characters in some places. You do not
    > say what this program is doing wrong. How can anybody help you?
    >
    > Please post a complete, working, short-as-possible, cut-and-pasted
    > program that demonstrates the problem you are having, but do it in
    > comp.lang.perl.misc because this newsgroup is defunct.
    >
    > Thanks.
    >
    >
    > ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet
    > News==----
    > http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000
    > Newsgroups
    > ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
    b.milbrandt, Apr 20, 2005
    #3
  4. b.milbrandt

    Jim Gibson Guest

    In article <Alg9e.2409$WI3.2174@attbi_s71>, b.milbrandt
    <> wrote:

    [top posting fixed]

    >
    > "Jim Gibson" <> wrote in message
    > news:190420051608396664%...
    > > In article <ttf9e.2474$c24.1712@attbi_s72>, b.milbrandt
    > > <> wrote:
    > >


    [ original program with bugs snipped]

    > > Is this the actual program you are trying to run? You are missing
    > > characters and using the wrong characters in some places. You do not
    > > say what this program is doing wrong. How can anybody help you?
    > >
    > > Please post a complete, working, short-as-possible, cut-and-pasted
    > > program that demonstrates the problem you are having, but do it in
    > > comp.lang.perl.misc because this newsgroup is defunct.
    > >
    > > Thanks.


    You have ignored my advice to post your question to
    comp.lang.perl.misc, I see. The only additional piece of advice I will
    give you is to recommend you put the following:

    use strict;
    use warnings;

    at the beginning of your program. You will then need to put 'my' in
    front of your variable declarations. After doing that, you should be
    able to find the source of your problems.

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Jim Gibson, Apr 20, 2005
    #4
  5. b.milbrandt

    Joe Smith Guest

    b.milbrandt wrote:

    > opendir DH, "/$target";
    > foreach $file (readdir DH)
    > { printf " the file name is %s\n", $file;


    That's wrong.

    foreach my $entry (readdir DH) {
    my $file = "/$target/$entry";
    print " the file name is $file\n";
    }

    You've got another error where you use @ARGV[0] instead of $ARGV[0].
    A better way would be to change
    $source = @ARGV[0];
    $target = @ARGV[1];
    to
    die "Usage: ..." unless @ARGV == 2;
    my $source = shift;
    my $target = shift;

    Any other questions should be posted to comp.lang.perl.misc and
    not this newsgroup (comp.lang.perl). But don't post anything
    until after you've read and understood the "Posting Guidelines for
    comp.lang.perl.misc".
    -Joe
    Joe Smith, Apr 21, 2005
    #5
    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. Alex
    Replies:
    2
    Views:
    1,204
  2. Steve
    Replies:
    3
    Views:
    26,946
    Steve
    Aug 25, 2006
  3. Replies:
    26
    Views:
    2,092
    Roland Pibinger
    Sep 1, 2006
  4. llothar
    Replies:
    17
    Views:
    1,243
    Steve Holden
    Apr 7, 2008
  5. MaggotChild

    File::Copy::copy With File Handles

    MaggotChild, Oct 18, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    493
    Ilya Zakharevich
    Oct 22, 2011
Loading...

Share This Page