Copy and change file extension

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • b.milbrandt

    Copy and change file extension

    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;
    }


  • Jim Gibson

    #2
    Re: Copy and change file extension

    In article <ttf9e.2474$c24 .1712@attbi_s72 >, b.milbrandt
    <b.milbrandt@in sightbb.com> wrote:
    [color=blue]
    > 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;
    > }[/color]

    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 =---

    Comment

    • b.milbrandt

      #3
      Re: Copy and change file extension

      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" <jgibson@mail.a rc.nasa.gov> wrote in message
      news:1904200516 08396664%jgibso n@mail.arc.nasa .gov...[color=blue]
      > In article <ttf9e.2474$c24 .1712@attbi_s72 >, b.milbrandt
      > <b.milbrandt@in sightbb.com> wrote:
      >[color=green]
      >> 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;
      >> }[/color]
      >
      > 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 =---[/color]


      Comment

      • Jim Gibson

        #4
        Re: Copy and change file extension

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

        [top posting fixed]
        [color=blue]
        >
        > "Jim Gibson" <jgibson@mail.a rc.nasa.gov> wrote in message
        > news:1904200516 08396664%jgibso n@mail.arc.nasa .gov...[color=green]
        > > In article <ttf9e.2474$c24 .1712@attbi_s72 >, b.milbrandt
        > > <b.milbrandt@in sightbb.com> wrote:
        > >[/color][/color]

        [ original program with bugs snipped]
        [color=blue][color=green]
        > > 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.[/color][/color]

        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 =----

        Comment

        • Joe Smith

          #5
          Re: Copy and change file extension

          b.milbrandt wrote:
          [color=blue]
          > opendir DH, "/$target";
          > foreach $file (readdir DH)
          > { printf " the file name is %s\n", $file;[/color]

          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

          Comment

          Working...