Professional Web Applications Themes

Copying files from One PC to another. - PERL Beginners

Hello Listers. Please I am a beginner. I am making tentative steps to creating a script which will keep my Notebook and Workstation at work in sync. My first attempt is just to copy the files from the one to local. Here is my script - it works, but I suspect there should be a better way to do this "More Perl Correct" so to speak. use warnings; use strict; use File::Copy; while(defined(my $filename=glob("///code/*.*"))) { if ($filename =~ m/(\w+)\.(\w+)/){ my $main = $1; my $ext = $2; copy ("$filename", "c:/code/$main.$ext"); } } I would especially appreciate pointers on the regex, ...

  1. #1

    Default Copying files from One PC to another.

    Hello Listers.



    Please I am a beginner.

    I am making tentative steps to creating a script which will keep my
    Notebook and Workstation at work in sync.

    My first attempt is just to copy the files from the one to local.

    Here is my script - it works, but I suspect there should be a better way
    to do this "More Perl Correct" so to speak.



    use warnings;

    use strict;

    use File::Copy;



    while(defined(my $filename=glob("///code/*.*"))) {

    if ($filename =~ m/(\w+)\.(\w+)/){

    my $main = $1;

    my $ext = $2;

    copy ("$filename", "c:/code/$main.$ext");

    }

    }



    I would especially appreciate pointers on the regex, is there a better
    way to do this?

    All pointers will be appreciated.

    Many Thanks

    Denham


    Denham Guest

  2. #2

    Default Re: Copying files from One PC to another.

    Denham,
    As usual with Perl, one can express them self's as they wish. There really is no beter way to do any task, unless you have specific specifications/standards. Here is an alternative, using 'split' function.

    #!PERL -w
    use warnings;
    use strict;
    use File::Copy;

    my files=glob('C:\pie\bar\*.*');
    my ($file,$debug);

    for $file ( files ){
    no strict;
    next if -d $file; # skip directorys
    print "$file \t" if $debug;
    local ($main,$ext) = split /\./,$file;
    print "$main:$ext\n" if $debug
    copy ("$file, "c:/code/$main.$ext");

    }

    hth,
    Mark G.

    ----- Original Message -----
    From: Denham Eva <co.za>
    Date: Friday, September 10, 2004 11:09 am
    Subject: Copying files from One PC to another.
     

    mgoland@optonline.net Guest

  3. #3

    Default Re: Copying files from One PC to another.

    Denham Eva wrote: 

    Hello,
     

    Your regular expression is matching \w+\.\w+ however file names can contain
    characters that are not included in the \w character class. It would be
    better to use the File::Basename module to extract the file name from the path.

    use warnings;
    use strict;
    use File::Copy;
    use File::Basename;

    while ( glob '///code/*.*' ) {
    my $filename = basename( $_ );
    next unless $filename =~ /[^.]\.[^.]/;
    copy( $_, "c:/code/$filename" ) or die "Copy failed: $!";
    }
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  4. #4

    Default Re: Copying files from One PC to another.

    [ Please do not top-post. TIA ]


    net wrote: 
    > Denham,
    > As usual with Perl, one can express them self's as they wish. There really is no beter way to do any task, unless you have specific specifications/standards. Here is an alternative, using 'split' function.
    >
    > #!PERL -w
    > use warnings;[/ref]

    You probably shouldn't use both " -w" and "use warnings;". Read the
    perllexwarn man page for an explanation of the difference between the two.

    perldoc perllexwarn

     

    There is no *good* reason to use backslashes.

     

    You should declare $file here as its contents are only visible inside the for
    loop.

    for my $file ( files ) {

     

    There is *NO* *GOOD* reason to turn off strict!

     

    You should only use local() if you are stuck in Perl4 (ick!) or you need to
    localize one of perl's built-in variables.

     
    ^
    Syntax error, unmatched quote.

     






    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. Question problems copying or renaming files unlock delete files
    By anhgun123 in forum Brainstorming Area
    Replies: 1
    Last Post: February 27th, 11:34 AM
  2. Copying / Moving files
    By Colin in forum Macromedia Director Basics
    Replies: 7
    Last Post: June 18th, 02:33 PM
  3. Copying and moving files...
    By Brett in forum Microsoft SQL / MS SQL Server
    Replies: 1
    Last Post: July 15th, 04:00 PM
  4. Copying files from the cd?
    By Akhtar Ali webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 1
    Last Post: July 5th, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139