Perl command to copy one file into another

Discussion in 'Perl Misc' started by Bill, Sep 29, 2003.

  1. Bill

    Bill Guest

    while ( <INPUT1> ) {
    # print "Reading $ARGV[0]\n";
    print OUTPUT $_;
    }

    The copy statment, copy("file1","file2") or die "Copy failed: $!"; is
    supposed to replace the above while statement.

    I get this error with the copy statment ...
    C:\PERL1\OUTPUT7>bbbb5.pl output5.txt output6.txt output7.txt
    Undefined subroutine &main::copy called at C:\PERL1\OUTPUT7\bbbb5.pl line
    24.

    This is the script code ...

    open (INPUT1, "$ARGV[0]") or die "Cannot open $ARGV[0]";
    open (OUTPUT, "> $ARGV[2]");

    #print "First input file is: $ARGV[0]\n";
    #print "Second input file is: $ARGV[1]\n";
    #print "The output file is: $ARGV[2]\n\n";

    # This half of the code dumps everything in 'file1' to the output 'file3'
    # while ( <INPUT1> ) {
    # print "Reading $ARGV[0]\n";
    # print OUTPUT $_;
    # }

    File :: COPY;
    copy("$ARGV[0]","$ARGV[2]") or die "Copy failed: $!";

    # This half of the code compares 'file2' to 'file1' and writes out
    # any line that doesn't match to the output 'file3'

    open (INPUT1, "$ARGV[1]") or die "Cannot open $ARGV[1]";

    while ( <INPUT1> ) {
    $match = 0;

    $a = $_;
    open (INPUT2, "$ARGV[0]") or die "Cannot open $ARGV[0]";
    while ( <INPUT2> ) {
    $b = $_;
    if ($a eq $b) {
    $match = 1;
    last;
    }
    }

    if ($match == 0) {
    print OUTPUT $a;
    }
    }

    "ko" <> wrote in message
    news:bl37di$j2a$...
    > Bill wrote:
    > > Is there a single Perl command to copy file 1 into file 2 with out going
    > > through the where-with-all of a while loop? Thanks, Bill
    > >
    > > Perl command to copy one file into another file?
    > >
    > >

    >
    > The File::Copy module.
    >
    > use File::Copy;
    > copy("file1","file2") or die "Copy failed: $!";
    >
    > 'perldoc File::Copy' gives you all the details
    >
    > HTH - keith
    >
     
    Bill, Sep 29, 2003
    #1
    1. Advertising

  2. On Mon, 29 Sep 2003 22:55:51 GMT, Bill <> wrote:
    > while ( <INPUT1> ) {
    > # print "Reading $ARGV[0]\n";
    > print OUTPUT $_;
    > }
    >
    > The copy statment, copy("file1","file2") or die "Copy failed: $!"; is
    > supposed to replace the above while statement.
    >
    > I get this error with the copy statment ...
    > C:\PERL1\OUTPUT7>bbbb5.pl output5.txt output6.txt output7.txt
    > Undefined subroutine &main::copy called at C:\PERL1\OUTPUT7\bbbb5.pl line
    > 24.


    Read 'perldoc File::Copy' or example at bottom more carefully.
    You forgot the 'use' in 'use File::Copy;' (and also have extra spaces in
    it). Therefore, no copy sub has been defined.

    > This is the script code ...
    >
    > open (INPUT1, "$ARGV[0]") or die "Cannot open $ARGV[0]";
    > open (OUTPUT, "> $ARGV[2]");
    >
    > #print "First input file is: $ARGV[0]\n";
    > #print "Second input file is: $ARGV[1]\n";
    > #print "The output file is: $ARGV[2]\n\n";
    >
    > # This half of the code dumps everything in 'file1' to the output 'file3'
    > # while ( <INPUT1> ) {
    > # print "Reading $ARGV[0]\n";
    > # print OUTPUT $_;
    > # }
    >
    > File :: COPY;
    > copy("$ARGV[0]","$ARGV[2]") or die "Copy failed: $!";
    >
    > # This half of the code compares 'file2' to 'file1' and writes out
    > # any line that doesn't match to the output 'file3'
    >
    > open (INPUT1, "$ARGV[1]") or die "Cannot open $ARGV[1]";
    >
    > while ( <INPUT1> ) {
    > $match = 0;
    >
    > $a = $_;
    > open (INPUT2, "$ARGV[0]") or die "Cannot open $ARGV[0]";
    > while ( <INPUT2> ) {
    > $b = $_;
    > if ($a eq $b) {
    > $match = 1;
    > last;
    > }
    > }
    >
    > if ($match == 0) {
    > print OUTPUT $a;
    > }
    > }
    >
    > "ko" <> wrote in message
    > news:bl37di$j2a$...
    >> Bill wrote:
    >> > Is there a single Perl command to copy file 1 into file 2 with out going
    >> > through the where-with-all of a while loop? Thanks, Bill
    >> >
    >> > Perl command to copy one file into another file?
    >> >
    >> >

    >>
    >> The File::Copy module.
    >>
    >> use File::Copy;
    >> copy("file1","file2") or die "Copy failed: $!";
    >>
    >> 'perldoc File::Copy' gives you all the details
    >>
    >> HTH - keith
    >>

    >
    >
    >



    --
    David Efflandt - All spam ignored http://www.de-srv.com/
    http://www.autox.chicago.il.us/ http://www.berniesfloral.net/
    http://cgi-help.virtualave.net/ http://hammer.prohosting.com/~cgi-wiz/
     
    David Efflandt, Sep 30, 2003
    #2
    1. Advertising

  3. Bill wrote:
    >
    > File :: COPY;
    > copy("$ARGV[0]","$ARGV[2]") or die "Copy failed: $!";


    I don't -think- the first statement above will work.
    Try:
    use File::Copy;

    No extra spaces, no changes to uppercase, and the word
    "use" is important. I would also suggest putting THAT
    line near the beginning of your entire script, but I'm
    not sure it matters beyond style/readability.

    If that still doesn't work... read on.

    If you know EXACTLY what kind of system it will run on,
    (Windows, Linux, etc) you can use whatever command you
    would give to a shell or commandprompt.

    Use the ` command in perl to run the shell command:

    $result = `copy a.txt b.txt`;

    NOTE: The characters before "copy" and before the
    semi-colon are both backticks (key left of the "one"
    key on common US keyboards; lowercase tilde "~").

    $result will contain whatever output the shell returned
    (error messages, success message, whatever). Probably
    nothing if it was successful.

    Note that that method is not necessarily "portable".

    Mike
     
    Michael P. Broida, Sep 30, 2003
    #3
  4. Michael P. Broida <michael.p.broida@boeing_oops.com> wrote:

    > Use the ` command in perl to run the shell command:
    >
    > $result = `copy a.txt b.txt`;



    > $result will contain whatever output the shell returned

    ^^^^^^^^^^^^^^^^^^^^
    > (error messages,

    ^^^^^^^^^^^^^^


    bacticks do not capture STDERR.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 30, 2003
    #4
  5. Bill

    Bill Guest

    Abigail <> wrote in message

    > I actually prefer to shell out to copy files (but not by using backticks,
    > but by using system), because "cp" copies files in the way I expect, while
    > File::Copy doesn't. File::Copy doesn't respect execute bits on files,
    > "cp" does.


    Yeah, I have a wrapper around File::Copy that does this. Seems to me
    someone should write a rmscopy for Unix for that package.
     
    Bill, Sep 30, 2003
    #5
  6. Abigail wrote:
    >
    > Michael P. Broida (michael.p.broida@boeing_oops.com) wrote on MMMDCLXXXII
    > September MCMXCIII in <URL:news:3F78C9E5.500B843F@boeing_oops.com>:
    > () $result = `copy a.txt b.txt`;
    > ()
    > () $result will contain whatever output the shell returned
    > () (error messages, success message, whatever). Probably
    > () nothing if it was successful.
    >
    > Eh, no. $result will contain whatever the command wrote to
    > standard *output*. Error messages typically go to standard *error*
    > and that's is not collected by backticks - you'd first have to
    > merge them into the output stream.


    Oops, you're exactly correct. I wasn't specific enough.

    > () Note that that method is not necessarily "portable".
    >
    > I actually prefer to shell out to copy files (but not by using backticks,
    > but by using system), because "cp" copies files in the way I expect, while
    > File::Copy doesn't. File::Copy doesn't respect execute bits on files,
    > "cp" does.


    I agree that using a shell command can get you "better"
    results because it uses the "correct" method for that system.
    But if you're on Windows, "cp" doesn't do ANYTHING (unless
    you have Cygwin or similar installed). That's all I was
    referring to when I said it's not portable: you might have
    to change it for a different system.

    Thanks!
    Mike
     
    Michael P. Broida, Sep 30, 2003
    #6
  7. Tad McClellan wrote:
    >
    > Michael P. Broida <michael.p.broida@boeing_oops.com> wrote:
    >
    > > Use the ` command in perl to run the shell command:
    > >
    > > $result = `copy a.txt b.txt`;

    >
    > > $result will contain whatever output the shell returned

    > ^^^^^^^^^^^^^^^^^^^^
    > > (error messages,

    > ^^^^^^^^^^^^^^
    >
    > bacticks do not capture STDERR.


    You're correct. I didn't think it through far
    enough when I was writing that post.

    Thanks!
    Mike
     
    Michael P. Broida, Sep 30, 2003
    #7
    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,287
  2. Replies:
    26
    Views:
    2,173
    Roland Pibinger
    Sep 1, 2006
  3. tirzan
    Replies:
    5
    Views:
    994
    James Kanze
    Apr 2, 2010
  4. Bill
    Replies:
    8
    Views:
    2,467
    Jay Tilton
    Oct 1, 2003
  5. Replies:
    2
    Views:
    168
    Tad McClellan
    Mar 26, 2005
Loading...

Share This Page