Eshwar
Eshwar

Reputation: 1

how to copy the files form one folder to another with different extension with perl

#!/usr/bin/perl 
use File::Copy; 
print "content-type: text/html \n\n"; #The header 
$filetobecopied = "C:\Users\avinash\Desktop\mktg/"; 
$newfile = "C:\Users\avinash\Desktop\elp/"; 
copy("$.pdf","$.elp") or die "File cannot be copied.";

the above program i have used to get the out put but getting error can any one can help me to out in the code

Upvotes: 0

Views: 69

Answers (2)

Miller
Miller

Reputation: 35198

There are three big issues with your script.

  1. Always include use strict; and use warnings; in EVERY perl script.

    Using these two Pragmas is the number one thing that you can do to become a better programmer. Additionally, you'll always get more help from experts here on SO if they see you're doing this basic due diligence to track down errors yourself.

    In this case, you'll actually get two warnings in your code:

    Use of uninitialized value $. in concatenation (.) or string at script.pl line 6.
    Use of uninitialized value $. in concatenation (.) or string at script.pl line 6.
    

    So your line copy("$.pdf","$.elp") is interpolating the variable $. which is undefined because you haven't read from a file.

  2. Escape your backslashes in double quoted strings

    Backslashes have special meaning in literal string definitions. If you want a literal backslash in a double quoted string, then you need to escape it.

    In this instance the following are being translated:

    • \U is the uc function
    • \a is an alarm code
    • \D is just a literal D
    • etc.

    To fix this you either need to use a single quoted string or escape the backslashes

    my $filetobecopied = "C:\\Users\\avinash\\Desktop\\mktg"; # Backslashes escaped
    
    my $filetobecopied = 'C:\Users\avinash\Desktop\mktg';     # Single quotes safer
    

    Also, I can't figure out why you have a trailing forward slash in both of your strings.

  3. Output the error message: $!

    Always include as much information in your error messages as possible. In this case the File::Copy does the following:

    All functions return 1 on success, 0 on failure. $! will be set if an error was encountered.

    Therefore your or die statement should contain the following:

    copy("fromfile","tofile") or die "Can't copy: $!";
    

    For even better debugging information, you could include the parameters that you sent to copy:

    copy("fromfile","tofile") or die "Can't copy fromfile -> tofile: $!";
    

Anyway, these three things will help you debug your script. It's still not possible to completely interpret your intent based off the information you've provided, but the following is a stub of better formatting code:

#!/usr/bin/perl 
use strict;
use warnings;

use File::Copy; 

print "content-type: text/html \n\n"; #The header 

# The following is likely wrong, but the best interpretation of your intent for now:
my $filetobecopied = 'C:\Users\avinash\Desktop\mktg.pdf'; 
my $newfile        = 'C:\Users\avinash\Desktop\elp.elp'; 

copy($filetobecopied, $newfile)
    or die "Can't copy $filetobecopied -> $newfile: $!";

Upvotes: 0

choroba
choroba

Reputation: 241748

If you use backslashes, use single quotes for strings, or double the backslashes. In double quotes, many backslashed characters have special meanings:

my $newfile = "C:\Users\avinash\Desktop\elp/";
print $newfile;

Output:

C:SERSVINASHDESKTOPP/

There are some hidden characters, too:

0000000: 433a 5345 5253 0756 494e 4153 4844 4553  C:SERS.VINASHDES
0000010: 4b54 4f50 1b4c 502f                      KTOP.LP/

Upvotes: 1

Related Questions